0
votes

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: ':analysis_id/dataFunctions', resetNamespace: true }, 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})
});

The contents of routes/data-functions/index.js:

import Ember from 'ember';

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

The contents of routes/analyses/index.js:

import Ember from 'ember';

export default Ember.Route.extend({
  model() {
      return this.store.findAll("analysis");
    },

    setupController(controller, model) {
      controller.set("analyses", model);
    }

});

The contents of routes/analyses/show.js:

import Ember from 'ember';

export default Ember.Route.extend({
  model(params) {
    return this.store.findRecord('analysis', params.analysis_id);
  },

  setupController(controller, model) {
    controller.set("analysis", model);
  }
});

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 using Ember 2.0.1 and Ember Data 2.0.0. I am lost here, any help would be greatly appreciated.

2
this.store.findRecord("analysis", id) - see guides.emberjs.com/v2.0.0/models/the-rest-adapter - elithrar
Can you share code for route where you define model function with loaded analysis model? In which place you've tried using this.store.modelFor...? - Daniel Kmak
To access model of a different route you have to explicitly return it, inject it, or it has to be a nested leaf route - elrick
I changed the code around as you guys suggested but still cant find a way to make it work. - Rafael Barbosa
I think you should try answer of @Tyler Iguchi. - Daniel Kmak

2 Answers

1
votes

It's returning no mode found because you're returning a log statement in the dataFunctions route. Give this a try.

export default Ember.Route.extend({
  model(params) {
    return this.store.findRecord("analysis", params.analysis_id)
      .then( (analysis) => {
        return analysis.get('dataFuncitons');
      })
    }
});
1
votes

Ok, so went through the code there was a few issues. There was a typo in analysis, and the resetNamespace is making things act weird. Also removed some of the redundant path names.

Router.map(function() {
  this.route('analysis', function() {
    this.route('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');
    });
  });
});

Rename the dataFunctions model to data-function to reflect proper conventions, e.g. using singular and dasherizing.

The analysis model

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

The data-function model

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