How to acess model from another route

Hello there, I have this router.js

import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

Router.map(function() {
  this.route('analyses', function() {
    this.route('new', { path: 'new'});
    this.route('show', { path: ':analysis_id' });
    this.route('edit', { path: ':analysis_id/edit'});
  });

  this.route('dataFunctions', { path: 'analyses/:analysis_id/dataFunctions' }, function() {
    this.route('new', { path: 'new'});
  });
});

export default Router;

and these 2 models

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  dataFunctions: DS.hasMany('dataFunction', {async: true}),
});

and

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  analysis: DS.belongsTo('analysis', {async: true})
});

when I navigate to ‘/analyses/1/dataFunctions’ my analysis model is loaded (it is show in ember inspector) but I can’t seem to access it in my data-functions/index.js route. How do I go about this? I need the analysis model to extend findAll in my data-function adapter to change the url for a rails-api nested resource.

I tried using this.store.modelFor("analysis").get("id") but it errors saying get is not a funcion.

I am lost here, any help would be greatly appreciated.

Because your ‘dataFunctions’ route doesn’t reside within your ‘analyses’ route, even though the URL is nested, it is technically not a child of the ‘analyses’ route. Therefore, modelFor() will not work if you visit the ‘dataFunctions’ route directly. However, if you first visit one of the analyses new/show/edit routes first, then navigate to dataFunctions, then it should work. You should move the ‘dataFunctions’ route to within the ‘analyses’ route, at the same level as new/show/edit.

import Ember from 'ember';
import config from './config/environment';

var Router = Ember.Router.extend({
  location: config.locationType
});

Router.map(function() {
  this.route('analyses', function() {
    this.route('new', { path: 'new'});
    this.route('show', { path: ':analysis_id' });
    this.route('edit', { path: ':analysis_id/edit'});
    this.route('dataFunctions', { path: ':analysis_id/dataFunctions' }, function() {
      this.route('new');
    });
  });
});

export default Router;
1 Like

Thanks for the reply, but it still doesn’t work.

On routes/data-functions/index.js

import Ember from 'ember';

export default Ember.Route.extend({
  model() {
    console.log(this.store.modelFor("analysis").get("id"));
  }
});

still get the “get is not a function” error. =/

You want to use the name of the route, not the model: this.store.modelFor(“analyses”)

modelFor is on the route not store, so you have to do this.modelFor(‘analysis’) in your model hook.

(Sorry, on small phone)

With the router.js like this

Router.map(function() {   this.route('analyses', function() {
    this.route('new', { path: 'new'});
    this.route('show', { path: ':analysis_id' });
    this.route('edit', { path: ':analysis_id/edit'});
    this.route('dataFunctions', { path: ':analysis_id/dataFunctions', resetNamespace: true} , function() {
      this.route('new', { path: 'new'});
    });    
});

and routes/data-functions/index.js like this

import Ember from 'ember';

export default Ember.Route.extend({
  model() {
    let analysis = this.modelFor('analysis');
    // ...
  }
});

I tried analyses, analyses/show, analysis, show, analyses:show. Everytime it returns undefined. Ember inspector is showing the analysis loaded and I can’t access it :sadface:

d’oh! Indeed, i screwed that up doubly: this.modelFor(“analyses/show”)

Isn’t it analyses.show ?

@IanVS LOLZ! For my next act I’ll toot my horn about mi terifik speling.

1 Like

@escobera did you get it working? Basically what @brian_ally said: make the datafunctions a nested route, this will make sure the parent model is loaded and then in your nested route use this.modelFor(‘route name’) to get the model.

If you are not sure about the route name, you can see it in the ember inspector.