One View connected to multiple models

后端 未结 3 490
再見小時候
再見小時候 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:17

    jQuery's Deferreds work well here. As a crude example:

    var succesFunction = function () {
        console.log('success');
    };
    var errorFunction = function () {
        console.log('error');
    };
    
    $.when(taskModel.fetch(), userModel.fetch()).then(successFunction, errorFunction);
    

    You could also pipe the request through using the crude data (remember that fetch, save, create are really just wrappers around jQuery's $.ajax object.

    var taskModelDeferred = taskModel.fetch();
    var userModelDeferred = taskModelDeferred.pipe(function( data ) {
        return userModel.fetch({ data: { user: data.userId }});
    });
    

    note: Backbone returns the collection and model in the success / error functions by default on collections and models so if you need this be sure have a reference handy.

提交回复
热议问题