javascript – Backbone Marionette.js reqres框架不等待集合填充
作者:互联网
我正在使用Backbone.Marionette的请求 – 响应框架来获取集合中的数据,然后将其响应给请求它的请求对象,但显然它不会等待集合被填充.这是我的代码:
这是它请求数据的地方:
// Module: Timeline, ListController
var employees = App.request('employee:timeline');
这是我设置我的处理程序的地方:
// Entities Module
App.reqres.setHandler('employee:timeline', function() {
return API.getEmployeesForTimeline();
});
这是我的API的功能:
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;
}
}
解决方法:
使用promise将结果传回:
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
});
标签:javascript,backbone-js,marionette,fetch,backbone-collections 来源: https://codeday.me/bug/20190725/1533329.html