【问题标题】:Backbone collection inside model模型内的主干集合
【发布时间】:2015-08-31 12:40:01
【问题描述】:

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

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.

})

【问题讨论】:

    标签: javascript backbone.js


    【解决方案1】:

    这将是短暂的历史

    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);
        }
    });
    

    【讨论】:

    • 我收到 TypeError: history is not a constructor at this.history = new history();我将其更改为 History() 但这没有帮助。
    • 我在文档中也找不到history。我们想在这里做什么?不过有一个叫previous的东西
    • @manu29.d 我知道有以前但以前只有更改属性的先前值。我想要的是以前更改的属性的列表/集合。例如,模型开始时是空的,如果我更改某些内容,我会将其添加到“历史记录”中。然后,如果我再次更改模型,“历史”将具有 2 个先前更改的属性/模型。
    猜你喜欢
    • 2015-05-30
    • 1970-01-01
    • 2014-12-09
    • 1970-01-01
    • 1970-01-01
    • 2015-09-05
    • 2015-03-13
    • 1970-01-01
    • 2012-12-25
    相关资源
    最近更新 更多