Using global variables in backbone.js

So, the first question I could not find the answer to. Could be enough reason to ask your first question. Sorry if the answer can be found outside the backbone.js area.

In the backbone.js application, I need to have access to several variables in different functions, so I need to use some global variable settings.

I am wondering if my current solution / good practice is acceptable. My IDE (IDEA) seems like this is not the case:

var MyModel = Backbone.Model.extend({

initialize:function(){
  var myGlobalVar, myOtherGlobalVar;//marked as unused local variable
},

myFunction:function() {          
      myGlobalVar = value;//marked as implicitly declared
      model.set({"mrJson": {"email": myGlobalVar}});
      model.save();
    });
  }
},

myOtherFunction:function() {          
      myOtherGlobalVar = otherValue;//marked as implicitly declared
      model.set({"mrJson": {"email": myGlobalVar, "other": myOtherGlobalVar}});
      model.save();
    });
  }
}
}

I tried to declare implicitly declared global variables, but this led to the fact that they were not accessible from this function.

Is there a way to handle these global variables in backbone.js?

+5
source share
1 answer

, , MyModel. ( ), :

var MyModel = Backbone.Model.extend({

myGlobalVar: null,
myOtherGlobalVar: null,

initialize:function(){
  console.log(this.myGlobalVar)
},
...
+14

All Articles