Route#model()

model (params, transition) Object|Promisepublic

Defined in packages/ember-routing/lib/system/route.js:1405

A hook you can implement to convert the URL into the model for this route.

App.Router.map(function() {
  this.route('post', { path: '/posts/:post_id' });
});

The model for the post route is store.findRecord('post', params.post_id).

By default, if your route has a dynamic segment ending in _id:

  • The model class is determined from the segment (post_id's class is App.Post)
  • The find method is called on the model class with the value of the dynamic segment.

Note that for routes with dynamic segments, this hook is not always executed. If the route is entered through a transition (e.g. when using the link-to Handlebars helper or the transitionTo method of routes), and a model context is already provided this hook is not called.

A model context does not include a primitive string or number, which does cause the model hook to be called.

Routes without dynamic segments will always execute the model hook.

// no dynamic segment, model hook always called
this.transitionTo('posts');

// model passed in, so model hook not called
thePost = store.findRecord('post', 1);
this.transitionTo('post', thePost);

// integer passed in, model hook is called
this.transitionTo('post', 1);

// model id passed in, model hook is called
// useful for forcing the hook to execute
thePost = store.findRecord('post', 1);
this.transitionTo('post', thePost.id);

This hook follows the asynchronous/promise semantics described in the documentation for beforeModel. In particular, if a promise returned from model fails, the error will be handled by the error hook on Ember.Route.

Example

App.PostRoute = Ember.Route.extend({
  model: function(params) {
    return this.store.findRecord('post', params.post_id);
  }
});

Parameters:

params Object
the parameters extracted from the URL
transition Transition

Returns:

Object|Promise
the model for this route. If a promise is returned, the transition will pause until the promise resolves, and the resolved value of the promise will be used as the model for this route.
doc_EmberJs
2016-11-30 16:53:09
Comments
Leave a Comment

Please login to continue.