我正在使用backbone.js构建移动应用程序。
我需要通过检查服务器来确保设置项目详细信息。在设置详细信息之前,应用无法继续。我使用.when()和延迟对象:
console.log('before when ');
$.when(that.setProjectDetailsWait()).done(function(data){
console.log('after when setProjectDetailsWait and feed domain is ');
console.log(feed_domain);
});
setProjectDetailsWait: function(){
require(["app/models/project"], function (projectModel) {
console.log('in setProjectDetailsWait');
project = new projectModel.Project({id:project_title});
return project.fetch({
api: true,
headers: {device_id:standard_device_id,api_key:standard_api_key},
success: function (data) {
console.log('in setProjectDetailsWait success');
flickr_user_id = data.get('flickr_user_id');
flickr_api_key = data.get('flickr_api_key');
feed_domain = data.get('feed_domain');
},
error: function(model, xhr, options){
console.log('in setProjectDetails error');
},
});
});
},
然而,输出是:
before when router.js:237
after when setProjectDetailsWait and feed domain is router.js:240
undefined router.js:241
now getting generic router.js:246
in setProjectDetailsWait router.js:733
in setProjectDetailsWait success
因此.when()中的所有内容都会立即执行,并且不会等待setProjectDetailsWait()到finsih。为什么这不起作用?
答案 0 :(得分:1)
问题是在setProjectDetailsWait()中使用require。当我删除require并在路由器的顶部使用它时,.when()工作