模型内的骨干集合

时间:2015-06-15 21:06:31

标签: javascript backbone.js

我是骨干的新手,我想知道是否有办法将集合中的先前模型保存为模型本身的属性。例如,

var history = Backbone.Collection.extend({});

var myModel = Backbone.Model.extend({
  defaults: {
         id: '',
         name: '',
         history: history //history is a collection of myModel
          },

  //override setter so when set method is called, it will save the previous model inside history collection.

})

1 个答案:

答案 0 :(得分:0)

这将是短暂的历史

var myModel = Backbone.Model.extend({    
    defaults:{
        id:''
    },
    constructor: function(){
       this.history = new Backbone.Collection();
    },

    set: function(){
      var args = Array.prototype.slice.call(arguments);
      this.history.add(this.toJSON());
      return Backbone.Model.prototype.set.apply(this, args);
    }
});