0

I am using ember-data and in a simple example of classes and students I have an API that is handling /classes and /classes/:class_id/students.

The classes API request is made without any problems, but when I try to list the students in the class no API call is made. Right now I am not manually making an API call in the student route, since I was under the impression that ember-data would make the call based on the model relationship.

models/class.js

import DS from 'ember-data';

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

models/student.js

import DS from 'ember-data';

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

Adapter

import DS from 'ember-data';

export default DS.RESTAdapter.extend({
  needs: ['service:session'],
  host: AppENV.APP.API_URL,
  headers: function() {
    return {
      'accept': 'application/json',
      'Content-Type': 'application/json',
      'x-app-sig': this.get("session.token"),
    };
  }.property("session.token")
});

Updates

controllers/class/index.js

import Ember from 'ember';

export default Ember.ObjectController.extend({
  // simplified query
  all: function() {
    return this.get('students');
  }
}

templates/class/index.hbs

<div class="panel__master-content">
  <div class="students panel__scrollable">

    {{#each all}}
      {{partial 'partials/student-link'}}
    {{else}}
      No students currently exist  <= what always gets rendered out
    {{/each}}
  </div>
</div>

router.js

this.resource('classes', function() {
  this.resource('class', { path: '/:class_id' }, function() {
    this.resource('student', { path: '/students/:student_id' }, function() {
      // other stuff
    });
  });
});

Update 2

In the ember relationship docs it says that it expects that the child ids (student) are returned with the parent. Is this required? Is Ember not smart enough to make the API request via the parent id?

chris
  • 4,332
  • 5
  • 41
  • 61
  • Your assumptions seem to be correct. Can you include your controller/template? – Steve H. Aug 14 '14 at 17:51
  • I added the controller, template and routes. One thing to keep in mind is that there is never an API call to the classes/:class_id/students url. – chris Aug 14 '14 at 19:00
  • 1
    Use links if you want Ember Data to connect to a non-standard url. http://stackoverflow.com/questions/24515729/how-to-use-ember-data-with-nested-resources/24517273#24517273 – Kingpin2k Aug 14 '14 at 22:56

0 Answers0