Ember模型和组件,获取组件来检测模型中的更改

Ember Model and Component, getting components to detect changes in models

本文关键字:模型 组件 检测 获取 Ember      更新时间:2023-09-26

我对检测模型变化的控制器和组件感到困惑和困惑。根据Ember官方文件——

默认情况下,从模型挂钩返回的值将被分配给关联控制器的模型属性。例如,如果App.PostsRoute从其模型挂钩返回对象,则该对象将被设置为App.PostsController的模型属性

因此,当模型在路由中发生变化时,控制器不应该更新吗?或者,通过外部函数异步更新吗?

App.IndexRoute = Ember.Route.extend({
    model: function(){
    App.set('localStore', this.get('store'));  
    App.localStore.createRecord('stats', {'name': 'cde'});
   return this.store.find("stats");
  }
});
App.IndexController  = Ember.Controller.extend({
    modelObs: function() {
        // Never triggered!
    console.log("CONTROLLER: model updated!");
}.property('model')
});
// Component with the controller's model property passed to it as localModel
// in the template
App.NewCompComponent = Ember.Component.extend({
    localModel: null,
    modelObs: function() {
    console.log("COMPONENT: Model updated!");
    }.property('localModel')

这里有一个Jsbin来说明这个问题——http://jsbin.com/tavis/3/edit

我可能做错了什么吗?如何让组件检测来自控制器的传递模型的变化,以及类似地,如何让控制器检测路线模型的变化?也许我错过了一两件事——感谢你的指点!感谢

除非使用计算属性,否则不会对其求值。他们被懒散地评价。

使用这些属性中的任何一个都将导致对它们进行评估,并生成日志。

http://jsbin.com/suheroya/1/edit

此外,重要的是要知道模型本身没有改变,这样计算的属性就不会被反复调用。模型上的属性正在更改,如果您希望反复调用计算的属性,则需要监视这些属性。