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?

Share Improve this question asked Jan 20, 2012 at 15:40 ParalifeParalife 6,2368 gold badges42 silver badges65 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

If 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.

本文标签: