骨干-聆听2种模型的最佳方法是什么?

时间:2018-12-13 19:49:45

标签: javascript backbone.js

我正在一个项目上,我很好奇什么是侦听同一功能中使用的2个对象的最佳方法?

例如:

this.model1 = someModel();
this.model2 = someModel();

this.listenTo(this.model1, "sync", this.someFunction); // how can I get listenTo to listen for both changes before calling the function?
this.listenTo(this.model2, "sync", this.someFunction);

someFunction: function() {
    this.sumValue = (this.model1.get('value') + this.model2.get('value));
}

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

创建基本模型并从其他模型继承:

    var DataModel = Backbone.Model.extend({
        // ToDo
    });

    var FileModel = DataModel.extend({
        // ToDo
    });

    var FolderModel = DataModel.extend({
        // ToDo
    });

使集合的模型类型与基本模型相同:

    var DataCollection = Backbone.Collection.extend({
        model: DataModel
    });