主干模型 - >集合 - >模型 - >更新 - 不会触发根模型上的更改



我有这个

ServerInfoModel = Backbone.RelationalModel.extend({     
    relations: [
        {
            type: Backbone.HasMany,
            key: 'data',
            relatedModel: 'Data',
            collectionType: 'DataCollection',
            reverseRelation: {
                key: 'id',
                includeInJSON: 'id'
            }
        }
    ]
})
Data = Backbone.RelationalModel.extend({
});
DataCollection = Backbone.Collection.extend({
    model: Data
});

现在我有了一个与ServerInfoModel关联的视图。如果我更改该集合中模型Data的值,则ServerInfoModel上不会触发"更改",仅在DataCollection上触发;

如何将"更改"事件从DataCollection传递到ServerInfoModel?

我发现了如何做到这一点,只需倾听"数据"的变化并触发的"变化"

ServerInfoModel = Backbone.BaseRelationalModel.extend({
url: function () {
    return "/examples/serverInfo.json"
},
setup: function () {
    var self= this;
    this.get('data').on('change', function () {
        self.trigger("change");
    });
},
relations: [
    {
        type: Backbone.HasMany,
        key: 'data',
        relatedModel: 'Data',
        collectionType: 'DataCollection',
        reverseRelation: {
            key: 'id',
            includeInJSON: 'id'
        }
    }
]
});
Data = Backbone.BaseRelationalModel.extend({ 
});
DataCollection = Backbone.Collection.extend({
  model: Data
});

相关内容

  • 没有找到相关文章