更新模型时模板输出未更新

时间:2014-09-02 08:48:22

标签: javascript ember.js

我是Ember.js的新手,我正在尝试更新模型中的状态变量,但是当我更改它时,视图不会更新。

这是我正在尝试做的javascript代码:

App = Ember.Application.create({});

App.EventBus = Ember.Object.extend({
    eventBus: null,
    connect: function() {
        Ember.run.later(this, function() {
            App.Sessiondata.connection.status = "Online";
            console.log("status: " + App.Sessiondata.connection.status)
        }, 2000)
    }.on('init')
});

App.Router.map(function() {
    this.resource('home', { path: '/' });
});

App.Sessiondata = {
    connection : {
        status: 'Offline',
        statusClass: 'alert-error',
        retryInterval: 20000,
        maxAttempts: 5
    },
    user : {
        fullname: 'Dummy User',
        email: 'dummy@email.com'
    },
};

App.ApplicationRoute = Ember.Route.extend({
    model: function() {
        return App.Sessiondata;
    }
});

App.ApplicationController = Ember.ObjectController.extend({
    eventbus: App.EventBus.create()
})

这是jsfiddle:http://jsfiddle.net/k82Lhj0j/

想法是在事件总线打开后更新连接状态。 对于这个小测试,我忽略了真正的eventbus代码(它确实有效,但与此测试无关),只需使用run.later来模拟类似的行为。

如您所见,结果始终为“状态:离线”,而在2秒后它应变为“状态:在线”。

我可能在Ember.js如何运作的精神上做错了什么,但我没有看到它。

1 个答案:

答案 0 :(得分:0)

您正在修改普通的javascript对象,而不是Ember对象。如果你没有使用ember的'set'方法,Ember就不会注意到属性被更改,模板需要更新。 这有效(简化了您的示例):

http://emberjs.jsbin.com/yaliyo/1/edit

话虽这么说,你现在必须处理另一个问题才能找到存储你的Sessiondata等的地方.ApplicationController是一个选项,但可能不是最好的。