I have this:
var SomeNamespace = {
Model: Backbone.Model.extend(),
View: Backbone.View.extend({
model: new this.Model, //<-- how do I properly create a new object from parent to h开发者_开发技巧ere?
myMethod: function()
{
alert(this.model.MyName);
}
})
}
Where I added the comment, Model
is not defined, I understand why, what I want to know is, how do I access the Model
property of the parent?
var SomeNamespace = {
Model: Backbone.Model.extend(),
View: Backbone.View.extend({
model: null, //<-- how do I properly create a new object from parent to here?
myMethod: function()
{
alert(this.model.MyName);
},
init: function(){//runs when creating an instance of SomeNamespace.View
this.model = new SomeNamespace.Model();
}
})
}
because the Model is not defined yet, as opposed to other languages, you can not do that in JS. You need to initialize the var to Model in the constructor of the class.
'this', in your case refers to Backbone.View.extend i believe, you need to do the following:
model: new SomeNamespace.Model
Taken from the extend documentation for Backbone.js (http://documentcloud.github.com/backbone/#Model-extend):
Brief aside on super: JavaScript does not provide a simple way to call super — the function of the same name defined higher on the prototype chain. If you override a core function like set, or save, and you want to invoke the parent object's implementation, you'll have to explicitly call it, along these lines:
var Note = Backbone.Model.extend({
set: function(attributes, options) {
Backbone.Model.prototype.set.call(this, attributes, options);
...
}
});
Does this work?
Backbone.Model.prototype.model.MyName;
精彩评论