In Ember, how do I defer availability and bring the AJAX result to the controller?

I understand that Ember.Application is now deferReadiness , which allows me to wait for the AJAX call to return before the application initializes. However, in the example in api docs, they put the value in a global variable in the App:

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

Instead of entering a global variable for the token, I want to put the return value in my ApplicationController. However, I cannot find how to get the handle of the controller at the moment, i.e. in the jQuery callback.

+5
source share
1 answer

You can reopen your controller in the $.getJSON callback to set the response value in the token property. Assuming you have a ~/auth-token endpoint returning JSON with a single key attribute, you could 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 )

+9
source

All Articles