2014-04-18 58 views
0

我正在尝试使用简单的hasMany ember-data关联。为sideload,ids设置准备的数据。但是,结果的长度是零。并没有显示数据。Ember Data hasMany sideload返回0长度

JSBin:从这个JSBin http://jsbin.com/OxIDiVU/378/edit

代码:

App = Ember.Application.create(); 

App.Router.map(function() { 
    this.resource('flags'); 
}); 

App.FlagsRoute = Ember.Route.extend({ 
    model: function() { 
    return this.store.find('flag'); 
    } 
}); 

App.FlagsController = Ember.ArrayController.extend({ 
}); 

App.ApplicationController = Ember.Controller.extend({ 
    init: function() { 
    this.transitionToRoute('flags'); 
    } 
}); 

App.ApplicationAdapter= DS.RESTAdapter; 

App.Flag = DS.Model.extend({ 
    country: DS.attr('string'), 
    colors: DS.hasMany('color') 
}); 

App.Color = DS.Model.extend({ 
    name: DS.attr() 
}); 

$.mockjax({ 
    url: '/flags', 
    dataType: 'json', 
    responseText: { 
    flags: [ 
     { 
     id: 1, 
     country: 'Germany', 
     color_ids: [1, 2, 3] 
     }, 
     { 
     id: 2, 
     country: 'Russia', 
     color_ids: [2, 4, 5] 
     }, 
     { 
     id: 3, 
     country: 'USA', 
     color_ids: [2, 4, 5] 
     } 
    ], 
    colors: [ 
     { 
     id: 1, 
     name: "black" 
     }, 
     { 
     id: 2, 
     name: "red" 
     }, 
     { 
     id: 3, 
     name: "yellow" 
     }, 
     { 
     id: 4, 
     name: "white" 
     }, 
     { 
     id: 5, 
     color: "blue" 
     } 
    ] 
    } 
}); 

    <script type="text/x-handlebars" data-template-name="flags"> 
    <table class="table table-bordered table-striped"> 
     <thead> 
     <tr> 
      <th>Country Name</th> 
      <th>Flag colors number</th> 
      <th>Flag color names</th> 
     </tr> 
     </thead> 
     <tbody> 
     {{#each}} 
     <tr> 
      <td>{{country}}</td> 
      <td>{{colors.length}}</td> 
      <td> 
      {{#each color in colors}} 
       {{color.name}}<br> 
      {{/each}} 
      </td> 
     </tr> 
     {{/each}} 
     </tbody> 
    </table> 
    </script> 

回答

1

结帐上JSON conventions部分。问题是,余烬预计colors,而不是color_ids

http://jsbin.com/OxIDiVU/380/

flags: [ 
    { 
    id: 1, 
    country: 'Germany', 
    colors: [1, 2, 3] 
    }, 
    { 
    id: 2, 
    country: 'Russia', 
    colors: [2, 4, 5] 
    }, 
    { 
    id: 3, 
    country: 'USA', 
    colors: [2, 4, 5] 
    } 
], 
+0

谢谢!我之前检查过这一部分,但错过了这一点。真正的问题是,传递color_ids是ActiveModel :: Serializers的默认行为,很容易忽略。 –

+0

我现在使用DS.ActiveModelSerializer,所有这些问题都解决了。它需要一些修补,修复hasMany序列化,但很容易做到。 –

+0

你能解释一下如何修补它吗?我为类似的问题提出了一个问题。 http://stackoverflow.com/questions/23823850/emberjs-data-hasmany-sideloading-with-activemodelserializers – kunerd