Just like defining “show” routes in Ember has been conventionalized, it would be nice to conventionalize the index route as well.
For instance, the following pattern has been conventionalized to remove the model
hook on the PostRoute
:
App.Router.map(function() {
this.resource('posts');
this.resource('post', { path: '/post/:post_id' });
});
App.PostRoute = Ember.Route.extend({
model: function(params) {
return this.store.find('post', params.post_id);
}
});
However, the PostsRoute
still needs to define the model
hook:
App.PostsRoute = Ember.Route.extend({
model: function() {
return this.store.find('post');
}
});
I could understand how conventionalizing finding all records for the PostsRoute
and removing the model
hook would be too strong of an opinion, but potentially it makes sense on the PostsIndexRoute
.
To create the PostsIndexRoute
, a function must be passed as an argument:
App.Router.map(function() {
this.resource('posts', function() {});
});
For which, the PostsIndexRoute
would currently look like this:
App.PostsIndexRoute = Ember.Route.extend({
model: function() {
return this.store.find('post');
}
});
I’m proposing to conventionalize this pattern so the PostsIndexRoute
would not need a model
hook.
The Proposition
I’m actually proposing two things, for which I could submit an initial pull request.
*IndexRoute
Always create the *IndexRoute subroute when defining a resource so you don’t have to pass an empty function:
App.Router.map(function() {
this.resource('posts');
});
The above would create PostsRoute
, PostsIndexRoute
(with a default model
hook), and prospectively, the related PostsLoadingRoute
and PostsErrorRoute
.
Fetching
Conventionalize the *IndexRoute hook to function like routes with resource_id dynamic segments by introducing common model
hook behavior on current routes like the PostsRoute
:
App.PostsIndexRoute = Ember.Route.extend({
model: function() {
return this.store.find('post');
}
});
Thus, removing the model
hook so the route looks more like:
App.PostsIndexRoute = Ember.Route.extend({});
Which, of course, allows us to omit the PostsIndexRoute
definition altogether.