Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update Ember.Route model dynamically?

Tags:

ember.js

I have a route like this:

App.PeopleRoute = Ember.Route.extend({
  model: function()
  {
    return App.Persons.find(personId);
  }
});

where personId is loaded asynchronically and is a normal JavaScript variable outside Ember. Now when route is displayed it gets the current PersonId and displays proper data. But when i change the value of personId it does not update the view.

So my question is what is a way to refresh this route to find records with new personId?

like image 389
Tom Smykowski Avatar asked Aug 03 '13 10:08

Tom Smykowski


1 Answers

This is because model hook is executed only when entered via URL for routes with dynamic segments. Read more about it here.

The easiest solution for this would be to use transitionTo.

App.PeopleRoute = Ember.Route.extend({
  model: function(params)
  {
    return App.Persons.find(params.personId);
  },
  actions: {
    personChanged: function(person){
       this.transitionTo("people", person);
    }
  } 
});

App.PeopleController = Em.Controller.extend({
  observeID: function(){
     this.send("personChanged");
  }.observes("model.id");
});
like image 55
shime Avatar answered Nov 04 '22 23:11

shime