Get param of edit route (/foobar/2/edit)

71 views Asked by At

I wonder how to get the param (the ID) of my edit route in Ember.js.

That's how I defined my route:

this.resource('accounting', function() {

  this.resource('accounting.requests', { path: '/requests' }, function() {
    this.route('new');
    this.route('archived');
  });
  this.resource('accounting.request', { path: '/request/:request_id' }, function() {
    this.route('edit');
  });

});

Now I want to fetch the model by its ID on this edit route /accounting/request/3/edit:

App.AccountingRequestEditRoute = Ember.Route.extend({

  model: function(params) {
    return this.store.find('request', params.request_id);
  }

});

But this doesn't work because params is empty.

This works as expected, but the edit route doesn't:

App.AccountingRequestRoute = Ember.Route.extend({

  model: function(params) {
    return this.store.find('request', params.request_id);
  }

});
1

There are 1 answers

0
MilkyWayJoe On BEST ANSWER

When you are in a child route you can use this.modelFor, example:

App.AccountingRequestEditRoute = Ember.Route.extend({

  model: function(params) {
    return this.modelFor('request');
  }

});

Because in order to go to request.edit, you have to provide an id or object to its parent resource, request either directly going to the route passing the url to the browser or indirectly by transition from other routes that happen to link to edit.