admin管理员组文章数量:1315061
I want to do:
var MyModel = Backbone.model.extend({
someProp: { ... },
.
.
});
but have
new MyModel().someProp === new MyModel().someProp
return false
as if i had done
function MyModel() {
this.someProp = {...};
}
I dont want to put the assignment this.someProp = {...};
in the initialize
method because if i subclass MyModel, i ll have to repeat the assignment also in the subclass's initialize method again or remember to call the parents initialize from the children initialize every time i subclass, which it seems to me as a workaround rather than a solution. So, is there any other way?
I want to do:
var MyModel = Backbone.model.extend({
someProp: { ... },
.
.
});
but have
new MyModel().someProp === new MyModel().someProp
return false
as if i had done
function MyModel() {
this.someProp = {...};
}
I dont want to put the assignment this.someProp = {...};
in the initialize
method because if i subclass MyModel, i ll have to repeat the assignment also in the subclass's initialize method again or remember to call the parents initialize from the children initialize every time i subclass, which it seems to me as a workaround rather than a solution. So, is there any other way?
2 Answers
Reset to default 8If you want to use it for multiple models, then one solution would be to create a standalone function that takes care of it, then you can call that function from the model's initialize method.
Here's an example
function addInstanceProperties() {
this.someProp = 'hello';
this.otherProp = 'world';
}
var Parent = Backbone.Model.extend({
initialize: function() {
//have to use 'call' so the context is set to this model
addInstanceProperties.call(this);
}
});
var Child = Parent.extend({
initialize: function() {
addInstanceProperties.call(this);
}
});
However, this isn't any different than having the property assignments in the parent and having the child call the parent's initialize method. And there is less code involved.
var Parent = Backbone.Model.extend({
initialize: function() {
this.someProp = 'hello';
this.otherProp = 'world';
}
});
var Child = Parent.extend({
initialize: function() {
Parent.prototype.initialize.call(this);
}
});
Unless you want to repeat the property assignments in each initialize method, this is really the only other way to do it in Javascript.
I would actually remend overriding the constructor in the parent class instead creating a separate function as Paul has suggested.
That way, if you do need a custom initialization function in child functions, the constructor retains the instance properties and you don't need to worry about managing scope or calling some global function, passing in this
context, etc.
var Parent = Backbone.Model.extend({
constructor: function(options){
this.someProp = 'hello';
this.otherProp = 'world';
Backbone.Model.prototype.constructor.apply(this, arguments);
}
});
var child = Parent.extend({
initialize: function(){
// don't need to worry about setting up instance props anymore.
// don't need to worry about invoking the overridden init method.
}
});
This is the same idea as what Paul has done in his second block of code, except that the constructor is separated from initialize in backbone for this exact reason -- so that you can override / customize initialization actions without needing to copy/paste redundant construction options everytime do you do so.
本文标签:
版权声明:本文标题:javascript - When extending a backbone model or view, how can i create properties that get created on the instance and not on th 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741970824a2407826.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论