dev

Learn how to use modelFor correctly in Ember.js

Using Modelfor correctly in Ember.js

The modelFor method needs the route defined according to its hierarchy. So if router.js looks like the following:

Router.map(function() {
                    this.route('org', {path: 'org/:id'}, function() {
                      this.route('repos');
                      this.route('repo', {path: ':repoid'}, function() {
                        this.route('issues');
                      });
                    });
                  });

Here's how to access repo's model in the `issues` route:

model() {
                    let repoId = this.modelFor('org.repo').repoid;

The first parameter needs the route according to its position in the hierarchy separated by a period.

Just writing `this.modelFor('repo')` will return undefined because that's not a route specified in the upper most level of the router. It's a child route and must be written out as so.

Thank you for your time.
Did this post bring you value?
Please share it on Twitter and subscribe to the email newsletter.
Thank you for reading.