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/