javascript - Backbone Marionette.js reqres framework doesn't wait for collection to get populated - Stack Overflow

I'm using Backbone.Marionette's request-response framework to fetch data in collection and th

I'm using Backbone.Marionette's request-response framework to fetch data in collection and then response it to the request object that request it, but obviously it doesn't wait for collection to get populated. And here is my code:

This is where it is requesting the data:

// Module: Timeline, ListController
var employees = App.request('employee:timeline');

and here is where i set my handler:

// Entities Module
App.reqres.setHandler('employee:timeline', function() {
    return API.getEmployeesForTimeline();
});

and here is my API's function:

getEmployeesForTimeline: function() {
    var employees = new Entities.EmployeeCollection();

    employees.fetch({
        success: function(employees) {
            returnEmployees(employees);
        }
    });

    function returnEmployees(employees) {
        // doing some things with employees collection
        return leaves;
    }
}

I'm using Backbone.Marionette's request-response framework to fetch data in collection and then response it to the request object that request it, but obviously it doesn't wait for collection to get populated. And here is my code:

This is where it is requesting the data:

// Module: Timeline, ListController
var employees = App.request('employee:timeline');

and here is where i set my handler:

// Entities Module
App.reqres.setHandler('employee:timeline', function() {
    return API.getEmployeesForTimeline();
});

and here is my API's function:

getEmployeesForTimeline: function() {
    var employees = new Entities.EmployeeCollection();

    employees.fetch({
        success: function(employees) {
            returnEmployees(employees);
        }
    });

    function returnEmployees(employees) {
        // doing some things with employees collection
        return leaves;
    }
}
Share Improve this question edited Oct 8, 2013 at 12:46 Nikhil Agrawal 26.6k21 gold badges93 silver badges127 bronze badges asked Aug 27, 2013 at 14:18 fre2akfre2ak 3292 gold badges8 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

Use a promise to pass the results back:

getEmployeesForTimeline: function() {
    var employees = new Entities.EmployeeCollection();
    var deferred = $.Deferred();
    employees.fetch({
        success: deferred.resolve
    });

    return deferred.promise();
}

// Entities Module: UNCHANGED
App.reqres.setHandler('employee:timeline', function() {
    return API.getEmployeesForTimeline();
});

//request data
var promise = App.request('employee:timeline');
promise.done(function(employees){
    //use employees
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745278600a4620171.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信