javascript - Controller 在 Ember 中更改模型的事件?

标签 javascript ember.js

Controller 在实例化后初始化或为特定路由重新初始化时,是否应该使用事件或 Hook 来捕获?

我已经尝试过 init() - 但我的 Controller 在应用程序的生命周期内只实例化一次,但支持多种模型,具体取决于路由。我需要捕获的是 Controller 模型发生变化的时间,或者实例化后路由发生变化的某个时刻。

我也试过设置一个计算属性,但它并不合适,因为我需要一个事件来在模型发生变化时触发一个函数——而不仅仅是在调用属性时。

非常感谢任何帮助。


编辑:啊哈!我完全检查了我可以在我的函数上设置 observes() 的事实,以便它会在模型​​更改时触发。

因此,我的回答是执行以下操作:

dependency: function() {
    console.log('the model has changed');

    //...other stuff...
}.observes('model')

最佳答案

来 self 的编辑:

Aha! I completely looked over the fact that I can set observes() on my function, so that it will fire when the model changes.

So, my answer was to do the following:

dependency: function() {
    console.log('the model has changed');

    //...other stuff... 
}.observes('model')

关于javascript - Controller 在 Ember 中更改模型的事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21840216/

相关文章:

javascript - 将 jpeg 图像转换为正确的字节数组时遇到问题

javascript - 如果 Internet Explorer 将打印页面大小调整为 60%

javascript - emberjs组件通信

ember.js - 在 View 中订阅模型事件的正确方法是什么?

ember.js - 使用 Ember 中的 Controller 存储用户对调查问卷的回答

javascript - 配置defaultNavigationOption headerBackImage会抛出undefined

javascript - 使用 jquery 或 javascript 在 wordpress 中获取 pagetitle

Javascript 检查两个数字数组中最接近的不同值

javascript - 将类绑定(bind)到 ember 中的 div

jquery - 仅限 Chrome 中的超慢预检选项