Why is there both a resource.index route and a resource route?
I have been trying to understand this for quite a while now and it is not clear to me. I am an AngularJS/Rails developer trying to make the switch to Ember on the front-end. So far, I am pretty frustrated with how difficult it is to understand how to best setup/organize a simple CRUD resource in Ember.
For example:
Router.map(function() {
this.resource('users', function() {
this.route('new');
this.route('show', { path: '/:user_id' });
this.route('edit', { path: '/:user_id/edit' });
});
});
First of all, is this the recommended way to create a āstandardā CRUD resource? One that works well with a Rails scaffolded resource as its API for example. Or should the resource be defined with the singular noun as in this.resource(āuserā)?
More importantly, which route handler should have the model hook? It works with the model hook put in either the UsersRoute or the UsersIndexRoute, but with some unexpected behavior in my opinion.
Option #1 - Use the UsersRoute
export default Ember.Route.extend({
model: function() {
return this.store.find('user');
},
actions: {
save: function() { ... }
}
});
This works fine but hitting the /users/:user_id URL makes a GET request to /users, so the server response contains all of the users. Also, this option creates a strange directory structure as follows:
app/routes/users/new.js
app/routes/users.js
Option #2 - Use the UsersIndexRoute
export default Ember.Route.extend({
model: function() {
return this.store.find('user');
},
actions: {
save: function() { ... } //Doesn't work from users/new or users/edit
}
});
This works fine as well but I am not sure why the model is inherited in the UsersNewRoute and UsersEditRoute. Is the UsersIndexRoute considered a parent route? It does not appear to be in the Ember Inspector. Lastly, why does the save action not work from here?
Any advice here will be greatly appreciated. I have always been attracted to Ember for its convention-over-configuration approach but this index route stuff does not seem to fit the bill.