I want to initialize my javascript class like this:
Example.init({"settings": {"baseUrl":"http://example.com"}, "user": {"id":123, "name":"john doe"} );
And then I want to be able to use it like this:
Example.settings.baseUrl
Example.user.id;
Example.user.name;
I am currently using the module pattern like this:
Example = (function(){
var _init = function(data) {
};
return {
init: function($data) {
_init($data);
}
};
})();
Example.module2 = (function(){
var _init = function(data) {
if(Example.user.id > 0) { // referencing data set in the Example.init
}
};
return {
init: function($data) {
_init($data);
}
};
})();
I'm not sure how I can expose these properties, looking for an explanation and guidance.
(please comment on best practise also, should I use $ for parameters and if so when?)
Note: Here is basic outline of what I am trying to do.
The first thing I will do is call the Example.init function in all my pages:
Example.init({"settings": {"baseUrl":"http://example.com"}, "user": {"id":123, "name":"john doe"} );
I want my other modules to be able to reference this data I set in the .init() method, see the Example.module2 I added above.
Is there a timing issue with this?