Everything I have read about scope in Meteor tells me that using the 'var' keyword in front of a variable declaration will give that variable 'file' scope whereas omitting the 'var' keyword will give it 'package' scope.
I don't think that's the whole story, though. I think that the only time variables have true 'file' scope is when they are declared outside of any brackets. Otherwise, their scope seems to be limited to within those brackets.
Even more confusing is an issue I am facing right now where properties of the same object have a different value depending on whether they are accessed from inside or outside of brackets, in this particular case a call to a Meteor method.
Here is some code to illustrate what I mean. I declare the object outside of any brackets and then access properties of it and assign other values to other properties of it from inside 'submit form', one of the events contained within 'Template.inputNamesForm.events'. First, I populate its 'inName' property with a string value contained in a text box inside my form as follows:
var name1 = {};
Template.inputNamesForm.events({
'submit form': function (event) {
event.preventDefault();
name1.inName = event.target.firstName.value;
Meteor.call('convertName', name1.inName, function (error, result) {
name1.outName = result;
console.log("Value as seen from inside call after button click: name1.inName = " + name1.inName + " -- name1.outName = " + name1.outName);
});
console.log("Value as seen from outside call after button click: name1.inName = " + name1.inName + " -- name1.outName = " + name1.outName);
}
)};
After assigning a string value to name1.inName, I use that property to invoke a call to a Meteor method. The output of that method is another string which I assign to a different property of the same object, ie. name1.outName.
My expectation is that the properties of this object will contain consistent values anywhere they are accessed from within this file. After all, they are supposed to have 'file' scope, aren't they?
What actually happens, though, is different. The value of name1.outName is different depending on whether I access it from inside the callback of my Meteor method call or outside of it.
If I call it from inside the callback, it has the expected value. If I call it from outside the callback, its value is undefined after the first button click. If the button is clicked again, however, then it will take a new value. It is always exactly one button click away from, ie. behind, where I would expect it to be, ie. from its value when called from within the callback.
Equally surprising is that the console.log command located after the Meteor method call gets executed before the console.log command which appears above it inside the callback.
I have to conclude that, if this parameter can have two values at any given time, then there must be two name1 objects, one existing inside the Meteor methods's callback brackets and another outside them. But where was this second version invoked/declared?
Another observation: Even declaring a variable as global by omitting the 'var' keyword doesn't seem to overcome the problem with items within brackets not being seen by items outside. I just really believe that scope in Meteor is a lot more complicated than anything I have read suggests. And I really wish there were some sort of comprehensive guide available for explaining just exactly how it works.
In any case, here is what I hope to get in the way of answers to this query:
I want to know how I can make the value of variables be consistent throughout the code. More importantly, I want values to be correct after just one button click, ie. I don't want users to have to click the button twice to get the right information.
I'd appreciate any help anyone can offer.
Meanwhile, I have checked these sources (among others):
https://medium.com/meteor-js/meteor-managing-the-global-namespace-5a50080a05ea#.t8gz1h3oa
http://blog.b123400.net/how-does-meteor-manage-scope/
I've also looked at these threads and several others, most of which have just confused me more:
Meteor variable scope (global, client, server, or all?)
Global variables in Meteor