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

前端 未结 1 973
旧时难觅i
旧时难觅i 2021-01-03 01:47

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

相关标签:
1条回答
  • 2021-01-03 02:46

    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)

    0 讨论(0)
提交回复
热议问题