I have the following Parent Object:
Context = {
ContextModel: Backbone.Model.extend({
//model Code
}),
ContextList:Backbone.Collection.extend({
model : Context.ContextModel
// collection Code
}),
Contexts: new Context.ContextList,
ContextView: Backbone.View.extend({
// view Code
})
}
In the above code,model : Context.ContextModel throws an error saying Uncaught ReferenceError: Context is not defined. I have defined the Context Object but somehow it does not see it. Could someone please help me out.
Thanks
Let’s look through the eyes of the JavaScript interpreter. You have a statement,
Context = { ... }. In order to execute that statement, it has to first construct the{ ... }so it can assign it toContext. In order to construct the{ ... }, it needs to evaluatenew Context.ContextList. Unfortunately, it’s still constructing the{ ... }part, and has not assigned anything toContextyet. Thus,Contextis undefined when you attempt to create a new instance ofContext.ContextList. You have the same problem trying to accessContext.ContextModelwhen creatingContext.ContextList. Try this: