One View connected to multiple models

后端 未结 3 484
再見小時候
再見小時候 2021-02-08 15:28

I have the following problem…

MyView which is connected to two views: TaskModel and UserModel

TaskModel = {id: 1,          


        
3条回答
  •  灰色年华
    2021-02-08 16:19

    I've run into this very same issue with a complex layout that used two models and multiple views. For that, instead of trying to synchronize the fetches, I simply used the "success" function of one model to invoke the fetch of the other. My views would listen only to the change of the second model. For instance:

    var model1 = Backbone.Model.extend({
        ...
    });
    var model2 = Backbone.Model.extend({
        ...
    });
    
    var view1 = Backbone.View.extend({
        ...
    });
    var view2 = Backbone.View.extend({
        ...
    });
    
    model2.on("change",view1.render, view1);
    model2.on("change",view2.render, view2);
    

    Then...

    model1.fetch({
        success : function() {
            model2.fetch();
        }
    });
    

    The point to this is you don't have to do any sophisticated synchronization. You simply cascade the fetches and respond to the last model's fetch.

提交回复
热议问题