Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember-data lazy load association with "links" attribute

I have a model Teacher which has many Students. The models are defined as follows:

App.Teacher = DS.Model.extend({
  email: DS.attr('string'),
  students: DS.hasMany('student')
});

App.Student = DS.Model.extend({
  teacher: DS.belongsTo('teacher'),
});

When a Teacher logs in, the server returns a JSON representation of the Teacher:

{
  id: 1,
  email: "[email protected]",
  links: {
    students: /teacher/1/students
  }
}

In the controller for login, I then push this data into the store and store it in a property of the Session controller:

this.set('currentUser', this.get('store').push('teacher', teacherJson))

I want to lazy-load the students association so I used the "links" format as defined in the API (http://emberjs.com/api/data/classes/DS.Store.html#method_push). So, ideally, whenever I would call

App.SessionController.get('currentUser').get('students')

it would load the associated students by sending a GET request to /teacher/1/students. But that never happens. Why is the request not triggered?

like image 789
yorbro Avatar asked Sep 15 '13 13:09

yorbro


1 Answers

Ok, I found the answer. I had to add a property async: true to the students association in the model for Teacher:

App.Teacher = DS.Model.extend({
  email: DS.attr('string'),
  students: DS.hasMany('student', { async: true })
});
like image 176
yorbro Avatar answered Nov 09 '22 06:11

yorbro