Ember数据未加载嵌套hasMany

时间:2014-05-06 04:17:29

标签: ruby-on-rails ember.js ember-data

问题

我有一个Rails 4支持的Ember应用程序需要迭代一些孩子,然后迭代那个孩子的孩子。设置是会话 - > hasMany - >注释 - > hasMany - >指标。

我可以加载/ session / 1 show模板并显示会话的属性。我还可以遍历会话的注释并显示注释的文本。但是,当我迭代注释的指标时,没有任何显示。如果我输出{{#with annotation}}{{indicators}}{{/with}},我只需<DS.PromiseArray:ember802>

Ember对/ sessions / 1和/ annotations进行AJAX调用?ids%5B%5D = 113&amp; ID%5B%5D = 112。但它永远不会打电话给/指标。

我已经看到其他帖子描述了同样的问题,但是这些问题的解决方案经常归结为骆驼套管等。在这种情况下,由于/指标永远不会被调用,我做错了什么?

环境

ember.js - 1.5.1  
ember-data.js - 1.0.0-beta.7  
Written in CoffeeScript  

Ember设置

Insight.ApplicationAdapter = DS.ActiveModelAdapter.extend({})  
Insight.ApplicationSerializer = DS.ActiveModelSerializer.extend({})

模型

App.Indicator = DS.Model.extend {
  title:      DS.attr 'string'
}

App.Annotation = DS.Model.extend {
  text:         DS.attr 'string'
  session:      DS.belongsTo 'session', inverse: 'annotations'
  indicators:   DS.hasMany 'indicator', async: true
}

App.Session = DS.Model.extend {
  subject: DS.attr 'string'
  students: DS.attr 'number'
  time: DS.attr 'string'
  annotations: DS.hasMany 'annotation', async: true
}

路线

App.SessionRoute = Ember.Route.extend {
  model: (params)->
    return @store.find('session', params.session_id)`

会话显示模板(相关部分)

<section class='content'>
  {{#each annotation in annotations itemController="annotation"}}
    {{#with annotation}}
      <li {{bind-attr class="isCompleted:completed isEditing:editing"}}>
          {{#if isEditing}}
            {{edit-annotation class="edit" value=bufferedText focus-out="doneEditing" insert-newline="doneEditing" escape-press="cancelEditing"}}
          {{else}}
              {{text}}
            </div>            
          {{/if}}
          {{#each indicator in indicators}}
            <button>{{indicator.title}}</button>
          {{/each}}
      </li>
    {{/with}}
  {{/each}}
  {{view Ember.TextField id="new-annotation" placeholder="Enter an annotation" valueBinding="newAnnotation" action="createAnnotation"}}
</section>

JSON GET Payloads

{"session":{
  "id":4,
  "subject":"Name of Subject",
  "students":1,
  "time":"08:52",
  "annotations":[113,112]}
}

{"annotations":
  [
    { "id":112,
      "text":"this is my first annotation",
      "session":4,
      "indicators":[1]
    },
    { "id":113,
      "text":"This annotation has indicators",
      "session":4,
      "team_member":8,
      "indicators":[1,2]
    }
  ]
}

1 个答案:

答案 0 :(得分:0)

问题在于JSON格式。

您正在使用的格式是RESTAdapter

接受的格式
data: [
  title: "Some Title",
  items: [1,2]
]

然而,ActiveModelAdapter期望数据采用以下格式:

data: [
  title: "Some Title",
  item_ids: [1,2]
]

要解决此问题,您可以切换到RESTAdapter或更改数据生成器以生成正确的格式。

JSBin with above code made workingother question that reference the same issue