My problem is that i want to create object of model in javascript .I use backbone.js
I want to share model object globally because I need that object in many js files
(function(){
this.mymodel=Backbone.Model.extend({
----
----
});
return{
var colobj = new mymodel;
}
})
and I am sharing “colobj” object in another js file as follows
(function(){
var collectionobj = new colobj;
});
but this gives me error what is wrong in this example
First of all, you should change this line :
to
The thing is that
mymodelwas undefined and that may have been the roots of your problem.In addition, your first
colobjis an instance of your model, thus it is an object and not a constructor. That’s why the second time you try to instantiate it :it throws an error, because the browser thinks you’re doing something like :
I suggest you pass only the model definition the from one closure to another:
UPDATE
I’m very surprised to see that I’ve missed this before.
You are returning an invalid object :
I think that you probably meant to return an object that hash a
colobjproperty with the value of your model. If that’s the case, all you have to do is return this object :