Jump to content

global variable availability


jimfog
 Share

Recommended Posts

Below you will see code related to a backbone view(simplified)...I have not included some code so as to keep things as simple as possible-I think the omitted code will not affect what I am trying to convey here.

var EventView = Backbone.View.extend({        selestrt: null,        events: {"change #timesfrom": "changefrom",             },      dropfrom:function(selestrt,test)        {  selestrt;//global        },      changefrom:function(){       selestrt;//global         }});

I have declared a global variable selestrt so that his can be available at BOTH functions...and here is where the problem manifests itself.

The dropfrom function is used to build a dropdown menu(selestrt is a UNIX timestamp that is used to create the menu).

 

Changefrom is called when the user makes s selection from the dropdown menu(you can see also the code corresponding to the event)

#timesfrom is the id of the div where the menu is included.

 

Unfortunately when changefrom is called selestrt is considered undefined.

 

I did not include any HTML or prepare a fiddle for that matter cause I want to portray the problem as simple as possible-but if you consider it necessary I will do it.

Link to comment
Share on other sites

from what you've shown, selstart is not global. it's just a property of the object you are passing into Backbone.View.extend.

Link to comment
Share on other sites

from what you've shown, selstart is not global. it's just a property of the object you are passing into Backbone.View.extend.

This is the stackoverflow topic from where I got the above....http://stackoverflow.com/questions/9411896/using-global-variables-in-backbone-js#comment38377574_9411976

 

I have forgotten to put the variable inside the initialize function...still the problem remains.Here, how the code is now...again simplified:

 var EventView = Backbone.View.extend({        selestrt: null,        initialize: function() {            _.bindAll(this);       this.selestrt;            },      events: {"change #timesfrom": "changefrom",             },      dropfrom:function(selestrt,test)        {  selestrt;//global        },      changefrom:function(){       selestrt;//global         }});

To get an understanding of what I am trying to do:

 

When dropfrom is called,the dropdown menu appears with a preselected vaue...(the UNIX timestamp, which is stored at selestrt ).

 

I want this to available at the changefrom function,which(as stated) is called when the user goes to change/selects a value from the drop menu.

Link to comment
Share on other sites

What you are doing makes no sense to me. A global named selestrt cannot be accessed if you have a local property named...

 selestrt: null,

...also what is this line supposed to do?

this.selestrt;

...or what is this line supposed to do?

selestrt;//global
Link to comment
Share on other sites

 

What you are doing makes no sense to me. A global named selestrt cannot be accessed if you have a local property named...

 selestrt: null,

...also what is this line supposed to do?

this.selestrt;

...or what is this line supposed to do?

selestrt;//global

 

Regarding the first 2 lines...I just copied the code from StackOverflow and begun testing.

 

And about the last line....if you notice it is INSIDE the changefrom function,cause I wanted to be available there.

Of course there is more code there...I just omitted it for reasons of simplicity,the variable as it is there,"alone",of course it does not make sense.

I can post the whole code if you want.

Here is the error message as it appears in the console....http://1drv.ms/1sUJQoH

Link to comment
Share on other sites

nothing?

Link to comment
Share on other sites

I am not familiar with Backbone but you have selestrt declared as a property and also as a parameter and then you put it on a line by itself. Try...

console.log(this.selestrt);
Link to comment
Share on other sites

I am not familiar with Backbone but you have selestrt declared as a property and also as a parameter and then you put it on a line by itself.

As already stated, I just followed the stackoverflow post...

Maybe I should prepare a jsfiddle demo.

Link to comment
Share on other sites

If you want selestrt to be availible inside changefrom(), simply prepend it with a "this."

 

they're both properties to the same object, so the "this" inside a function should point the the proper reference.

  changefrom:function(){    this.selestrt;      // NOT global, but a local variable to the       // object the function is assigned with  }

its not "technically" a global variable per se, but every function in the object can still access the variable.

 

I just read that stackoverflow link and even in their answer, they would use the "this" keyword. That's important.

Link to comment
Share on other sites

If you want selestrt to be availible inside changefrom(), simply prepend it with a "this."

 

they're both properties to the same object, so the "this" inside a function should point the the proper reference.

  changefrom:function(){    this.selestrt;      // NOT global, but a local variable to the       // object the function is assigned with  }

its not "technically" a global variable per se, but every function in the object can still access the variable.

 

I just read that stackoverflow link and even in their answer, they would use the "this" keyword. That's important.

Yes,your solution worked....by making a minor adjustment though:

 this.selestrt=selestrt;

P.S It had not occurred to me that backbone models are actually objects(with properties/methods)...I know that this can easily be deducted,nonetheless I hadn't noticed it(although it is apparent)

Edited by jimfog
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...