在Ember中,如何推迟准备,并将AJAX结果放入Controller? [英] In Ember, how to defer readiness, and put AJAX result into a Controller?

查看:101
本文介绍了在Ember中,如何推迟准备,并将AJAX结果放入Controller?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白,Ember.Application现在有延迟准确性,让我在初始化应用程序之前等待返回AJAX调用。然而,在api文档中的示例中,他们将该值放在App中的全局变量中:

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();
});

而不是为令牌引入全局变量,我想将返回的值放在我的ApplicationController中。但是,我似乎找不到如何在这一点上得到控制器的句柄,即在jQuery回调中。

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.

推荐答案

p>您可以 重新打开 您的控制器在 $。getJSON 回调中,在令牌属性中设置响应值。假设你有一个端点〜/ auth-token 返回一个单一属性的JSON,你可以做一些类似这个:

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();
});

(请参阅小提琴

这篇关于在Ember中,如何推迟准备,并将AJAX结果放入Controller?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆