Loading data through simple hasMany


#1

Hi,

I’m building a simple ToDo application to experiment with EmberData and I’m stuck loading data from an API through a hasMany relationship using the RESTadapter.

The models:

Gtd.Project = DS.Model.extend({
  name: DS.attr('string'),
  description: DS.attr('string'),
  tasks: DS.hasMany('task')
});

Gtd.Task = DS.Model.extend({
  name: DS.attr('string'),
  completed: DS.attr('boolean'),
  project: DS.belongsTo('project')
});

The route:

Gtd.ProjectsRoute = Ember.Route.extend({
  model: function() {
    return this.store.find('project');
  }
});

The view:

<ul>
        {{#each project in controller}}
          <li>{{project.name}} ({{project.tasks.length}})</li>
        {{/each}}
</ul>

… and the JSON.

{"projects":[
    {"id":1,"name":"Build Patio","details":null,"task_ids":[1,2]}
]}

The resulting UI is a list of projects with the tasks.length always 0. There isn’t an XHR to load the tasks, so I must be missing something which triggers that hasMany relationship to populate.

Thanks guys.


#2

I make a demo for you https://github.com/FeipingHunag/ember-todo


#3

Unfortunately that doesn’t seem to have any effect.


#4

@joshwalsh I’m having the same issues after upgrading to 1.0. I’ve posted a question on StackOverflow for this: http://stackoverflow.com/questions/18679113/ember-data-1-0-loading-hasmany-records-into-view and I’ve added a comment to this thread as well: Migrating from Ember Data 0.13 to 1.0.0 Beta 1 - My findings

Please keep us posted if you come up with a solution.


#5

I have a similar problem when I try to save the relationships. At that moment, they all disappear. I use Ember Data 1.0.0 Beta 2.

Might be related to issues:

I will watch resolution of these 2 issues and then test again. At this moment, hasMany relationship does not work in my case …


#6

@joshwalsh The response json format for hasMany records have changed between 0.13 and the 1.0 beta release. Instead of { projects: [ ... ,task_ids: [1, 2] ] } you need { projects: [ ... , tasks: [1, 2] ] } and need to return the comments with those ids, too, in the response.

See also the ember-data transition guide.


#7

Thanks for that. I missed that change in the transitions guide. That alone wasn’t enough to fix it, but I was able to pull the serializer in from this project to fix the hasMany in Ember-Data beta 2: