Ember.js 在Ember中,如何延迟准备就绪,并将AJAX结果放入控制器中?
我知道Ember.Application现在有一个功能,允许我在初始化应用程序之前等待AJAX调用的返回。但是,在api文档中的示例中,他们将值放入应用程序中的全局变量中:Ember.js 在Ember中,如何延迟准备就绪,并将AJAX结果放入控制器中?,ember.js,promise,Ember.js,Promise,我知道Ember.Application现在有一个功能,允许我在初始化应用程序之前等待AJAX调用的返回。但是,在api文档中的示例中,他们将值放入应用程序中的全局变量中: App = Ember.Application.create(); App.deferReadiness(); jQuery.getJSON("/auth-token", function(token) { App.token = token; App.advanceReadiness(); }); 我不想为令牌
App = Ember.Application.create();
App.deferReadiness();
jQuery.getJSON("/auth-token", function(token) {
App.token = token;
App.advanceReadiness();
});
我不想为令牌引入全局变量,而是想将返回值放入ApplicationController中。但是,此时我似乎找不到如何获取控制器的句柄,即在jQuery回调中。您可以在$.getJSON
回调中设置控制器,以在令牌
属性中设置响应值。假设您有一个端点~/auth令牌
返回带有单个属性键
的JSON,您可以执行以下操作:
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();
});
(见)我就知道会有办法!