Shagymoe
Shagymoe

Reputation: 1456

Params empty in model hook, but paramsFor is not empty

The example code below is how the model hook is supposed to work by default. Strangely, if I don't include the model hook at all, the model is populated correctly. If I include the model hook as below, it doesn't work because "params" is an empty object. However, this.paramsFor('somemodel') returns {somemodel_id: "1"} So, what am I missing here?

import Ember from 'ember';

export default Ember.Route.extend({
  model: function(params) {    
    return this.store.find('somemodel', params.somemodel_id);
  }
});

Upvotes: 2

Views: 529

Answers (2)

Patsy Issa
Patsy Issa

Reputation: 11303

Nested routes inherit the parent route's model if you do not specify a model hook. If all you are doing is looking up the model to edit you don't need a model hook, if you are querying the store for something else and need access to somemodel you can access it via this._super(...arguments).

export default Ember.Route.extend({
  model: function(params) {    
    return this.store.find('somemodel', this._super(...arguments).get('id'));
  }
});

Upvotes: 3

Shagymoe
Shagymoe

Reputation: 1456

It seems that params don't propagate to nested routes. My router looks like this:

  this.route('somemodel', { path: '/somemodels/:somemodel_id' }, function() {
    this.route('edit');
  });

The "index" route is implied and is the route that receives the params. The edit route is nested and does not receive the params.

Upvotes: 0

Related Questions