Reputation: 869
In Javascript, there exists a function which inherits the Backbone Model
window.MyModel = Backbone.Model.extend({ .. .. });
window.MyCollection = Backbone.Collection.extend({ .. .. });
In another, JS file we access this function as
var MyModelInstance = new window.MyModel();
Requirement is, all functions needs to be prefixed with specific global namespace (for example, company name 'Google') instead of generic keyword 'window'. How can we achieve it?
I tried the following, but with no success.
var googleNameSpace= defineNamespace("Google");
googleNameSpace.MyModel = Backbone.Model.extend({ .. .. });
googleNameSpace.MyCollection = Backbone.Collection.extend({ .. .. });
var MyModelInstance = new Google.MyModel();
Upvotes: 0
Views: 122
Reputation: 13379
in all files add this line at first
window.googleNameSpace = googleNameSpace || {};
update: ok you need google as namespace then have it like this
function defineNamespace(str) {
window[str] = window[str] || {};
return window[str];
}
googleNamespace = defineNamespace('Google');
will work now
Upvotes: 1
Reputation: 823
you don't need to prefix things with window
as this is assumed. you could define
window.myNamespace = Backbone.Model.extend({ .. .. });
and then refer to it only with:
myNameSpace.....
e.g.
window.myModule.helpers = {
....
};
can be referenced by:
myModule.helper.{{property/method}}
Upvotes: 0