Updating ember on model change

Using Ember 1.13

I have two nested resources, one of which renders a component based on the model returned by a dynamic route

something like

Router.map(function() {
  this.resource('maps', function () {
    this.resource('map', { path: '/:map_id' });
  });
});

      

and a template for the map that renders the component

map.hbs

{{some-component model=model}}
{{#each maps as |map|}}
  {{#link to 'map' map}}{{map.name}}{{/link-to}}
{{/each}}

      

when i first pressed

/maps/1

      

component displays

when i click one of the links and go to

/maps/2

      

it seems like the route never gets hit and the component never updates

is this a result of using a link-link or is it true that the route is not getting hit because just changing the model inside the route causes the same hooks for life to disappear?

What is the best way to force this component to reload?

+3


source to share


1 answer


You are probably doing something wrong.

Here's a basic working example:

<h3>maps.index</h3>

<ul>
{{#each model as |item|}}
  <li>
    {{link-to item.name 'maps.map' item}}
  </li>
{{/each}}
</ul>

      

<h3>maps.map</h3>

{{link-to "Back to index" 'maps.index'}}

<hr>

{{x-map map=model}}

      

<h4>components/x-map</h4>

<p>Id:   {{map.id}}</p>
<p>name: {{map.name}}</p>

      

App.Router.map(function() {
  this.route('maps', function () {
    this.route('map', { path: '/:map_id' });
  });
});

App.MapsIndexRoute = Ember.Route.extend({
  model: function () {
    return this.store.findAll('map');
  }
});

App.MapsMapRoute = Ember.Route.extend({
  model: function (params) {
    return this.store.findRecord('map', params.mapId);
  }
});

App.Map = DS.Model.extend({
  name: DS.attr('string')
});

      



Demo: http://emberjs.jsbin.com/voquba/4/edit?html,js,output

Note that instead of passing the entire entry to the child route:

{{link-to item.name 'maps.map' item}}

      

You can only transfer your ID:

{{link-to item.name 'maps.map' item.id}}

      

This is useful when you know the ID but do not have the entire record. Ember Data will search the store for the record of the given ID. If not present, it will retrieve it through the hook of the routing model.

+2


source







All Articles