Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js - Index route vs. "top-level" resource route?

Tags:

ember.js

When creating a "standard" crud resource in Ember.js, what is the difference between the index route and the "main" route?

For example, a users resource:

// app.router.js

Router.map(function() {
    this.resource('users', function() {
      this.route('new');
      this.route('show', { path: '/:user_id'});
      this.route('edit', { path: '/:user_id/edit'});
    });
});

Which route should contain the following "main" model hook?

// app/routes/users/index.js OR app/routes/users.js?

export default Ember.Route.extend({
    model: function() {
        return this.store.find('user');
    }
});

In other words, should I use the UsersRoute or the UsersIndexRoute? I find this very confusing. Any help is greatly appreciated.

like image 336
Corey Quillen Avatar asked Jan 29 '15 21:01

Corey Quillen


People also ask

What is index route in Ember?

Index Routes. At every level of nesting (including the top level), Ember automatically provides a route for the / path named index . To see when a new level of nesting occurs, check the router, whenever you see a function , that's a new level. For example, if you write a simple router like this: app/router.js Router.

What is route in Ember JS?

An Ember route is built with three parts: An entry in the Ember router ( /app/router. js ), which maps between our route name and a specific URI. A route handler file, which sets up what should happen when that route is loaded (app/routes/about.

What is outlet in Ember JS?

Here is the explanation: {{outlet}} -> This will provide a stub/hook/point into which you can render Components(Controller + View). One would use this with the render method of routes. In your case you will likely have a details route which could look like this.

What is Ember controller?

In Ember. js, controllers allow you to decorate your models with display logic. In general, your models will have properties that are saved to the server, while controllers will have properties that your app does not need to save to the server.


1 Answers

You should put a model hook to UsersRoute route if a given model is required to render all of the nested routes, i.e. index, new, edit and show, as it will be invoked when entering any of these routes.

If the model is required only when rendering / route, an not /:user_id routes, then it should be loaded in UsersIndexRoute route.

See JsBin for an example of when model hooks nested routes are being triggered.

For your use-case, it could be either way - if you would like to have master-child type of UI where list of users is rendered also when editing/showing/adding user, you should load and render list of users in UsersRoute, and then just render additional content in the nested route; if instead you do not want to render list of user in nested routes, just load and render them in UsersIndexRoute.

like image 84
jesenko Avatar answered Oct 20 '22 17:10

jesenko