Question

I am confused and in a fix with regards to controllers and components detecting changes in models. According to the Ember Official Documentation --

"By default, the value returned from your model hook will be assigned to the model property of the associated controller. For example, if your App.PostsRoute returns an object from its model hook, that object will be set as the model property of the App.PostsController."

Therefore, shouldn't the controller update when the model changes in the route or, asynchronously by way of an external function?

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')

Here's a Jsbin that illustrates the problem - http://jsbin.com/tavis/3/edit

Is there anything I might be doing wrong? How to get a component detect a change in a passed model from the controller, and similarly how to get the controller detect a change in the route's model? Perhaps I'm missing a thing or two -- pointers are appreciated! Thanks

Was it helpful?

Solution

Computed properties aren't evaluated unless they are used. They are lazily evaluated.

Using either of those properties will cause them to be evaluated and your logs will occur.

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

Additionally it's important to know that the model itself isn't changing, so that computed property won't be called over and over. Properties on the model are changing, and if you want the computed property to be called over and over you'll need to watch those properties.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top