In Ember, how to defer readiness, and put AJAX result into a Controller?

假装没事ソ 提交于 2019-12-12 07:16:10

问题


I understand that Ember.Application now has deferReadiness that lets me wait for the return of an AJAX call before initializing the app. However, in the example in the api docs, they put the value into a global variable in the App:

App = Ember.Application.create();
App.deferReadiness();

jQuery.getJSON("/auth-token", function(token) {
  App.token = token;
  App.advanceReadiness();
});

Rather than introducing a global variable for the token, I want to place the returned value into my ApplicationController. However, I can't seem to find how to get a handle to a controller at this point, i.e. in the jQuery callback.


回答1:


You can reopen your controller in the $.getJSON callback to set the response value in the token property. Assuming you have an endpoint ~/auth-token returning a JSON with a single attribute key, you can do something like this:

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)



来源:https://stackoverflow.com/questions/16205615/in-ember-how-to-defer-readiness-and-put-ajax-result-into-a-controller

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!