我知道 Ember.Application 现在有推迟准备状态这让我可以在初始化应用程序之前等待 AJAX 调用的返回。然而,在 api 文档的示例中,他们将值放入应用程序中的全局变量中:
App = Ember.Application.create();
App.deferReadiness();
jQuery.getJSON("/auth-token", function(token) {
App.token = token;
App.advanceReadiness();
});
我不想为令牌引入全局变量,而是想将返回值放入我的 ApplicationController 中。但是,我现在似乎无法找到如何获取控制器的句柄,即在 jQuery 回调中。
You can reopen你的控制器在$.getJSON
回调以设置响应值token
财产。假设你有一个端点~/auth-token
返回具有单个属性的 JSONkey
,你可以这样做:
window.App = Ember.Application.create();
App.ApplicationController = Em.Controller.extend({
token: ''
});
App.deferReadiness();
$.getJSON("/auth-token", function(token) {
console.log(token.key);
App.ApplicationController.reopen({
token: token.key
});
App.advanceReadiness();
});
(see fiddle)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)