frequent
frequent

Reputation: 28503

jquery widget factory - can I declare global variables at _create or outside the widget?

I'm trying to make my first Jquery widget using the widget factory.

I have 10 functions inside my widget and wanted to declare variables used throughout on _create like so:

(function($,window){
$.widget("mobile.somesome",$.mobile.widget, {

    _create: function() {           

        var self = this,
            that = something else, ...

I want to access these variables from within the other functions, but this does not work or more likely... I'm doing something wrong...

Question: Is it possible to declare variables on a "widget-global" level and if so how can I do it?

Thanks!

Upvotes: 0

Views: 4084

Answers (1)

CAbbott
CAbbott

Reputation: 8098

You could declare them as properties and them set/access them throughout the widget:

(function($) {
    $.widget("mywidget", {
        vars: {
            a: 1,
            b: 2 // etc.
        },

        _create: function() {  
            var x = this.vars.a;   
            this.vars.b = x + 1;   
        .
        .
        .

Upvotes: 2

Related Questions