简体   繁体   中英

Ember Data hasMany relationship empty result

I'm using:

  • ember-cli 0.2.7
  • ember-data 1.0.0-beta.18
  • ember 1.12.0

I'm not sure why but it seems that I can't retrieve the tags for my newsletter model.

I'm using ActiveModelAdapter :

import DS from 'ember-data';

export default DS.ActiveModelAdapter.extend({
  namespace: 'api/v1',
  host: 'http://localhost:3000'
});

newsletter.js

import DS from 'ember-data';

export default DS.Model.extend({
  title: DS.attr('string'),
  tags: DS.hasMany('tag')
});

tag.js

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string')
});

API response (rails backend using ActiveModelSerializer):

{
  "newsletters": [
    {
      "id": 1,
      "title": "Panel Weekly",
      "tag_ids": [
        1
      ]
    },
    {...}
  ],
  "tags": [
    {
      "id": 1,
      "name": "arts"
    },
    {...}
}

I don't know how to retrieve the list of tags for a newsletter. I tried this using the ember inspector and the console ($E containing the first newsletter):

>$E.get('tags.length')
0
>$E.get('tags')
Class {canonicalState: Array[0], store: Class, relationship: ember$data$lib$system$relationships$state$has$many$$ManyRelationship, record: Class, currentState: Array[0]…}
>$E.get('title')
"Panel Weekly"

I reproduced your situation in a JSBin and it works fine:

http://emberjs.jsbin.com/yeboqe/3/edit?html,js,output

Your problem must be somewhere else.

Ok so it turns out that I was using a custom serializer for my newsletter model that was not necessary. Just removing the file solved the problem ><.

import DS from 'ember-data';

// http://stackoverflow.com/a/21075106/1447468
export default DS.RESTSerializer.extend({
  serializeIntoHash: function(data, type, record, options) {
    var properties = this.serialize(record, options);
    for(var prop in properties){
      if(properties.hasOwnProperty(prop)){
        data[prop] = properties[prop];
      }
    }
  }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM