Ember Data hasMany sideload返回0长度

时间:2014-04-18 06:40:24

标签: ember.js ember-data

我试图使用简单的hasMany余烬数据关联。为侧载,ids集准备的数据。但是,结果的关联长度为零。并且数据没有显示。

JSBin:http://jsbin.com/OxIDiVU/378/edit

来自这个JSBin的代码:

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 个答案:

答案 0 :(得分:1)

查看JSON conventions上的部分。问题是,ember期望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]
  }
],