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
1 Answer
Reset to default 8Use 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条)