Ember得到了很多

时间:2014-09-01 06:03:15

标签: ember.js ember-data

我有模特

EmberApp.Card = DS.Model.extend({
  balance: DS.attr('number'),
  operations: DS.hasMany('operation', { async: true })
});

比我做

self.store.find('card', 'me')

响应

{ 
  "card": {
    "id": "53620486168e3e581cb5851a",
    "balance": 20
  }
}

获取卡片模型,但没有操作并将其设置为控制器道具" currentCard"
然后我想找到操作throw url / cards / me / operations
响应

{"operation": [{ "id": 1, "type": 1 }] }

我怎样才能从this.controllerFor(' *')获取。(' currentCard')...?

2 个答案:

答案 0 :(得分:1)

您实际上需要将您的操作作为卡片响应中的ID列表返回,如下所示:

{ 
  "card": {
    "id": "53620486168e3e581cb5851a",
    "balance": 20,
    "operations": [1, 2, 3]
  }
}

这将使您能够在另一条路线中执行此操作:

this.modelFor('card').get('operations');

或卡片控制器中的这个:

this.get('content.operations');

这将执行以下对API的调用:

/api/operations?ids[]=1&ids[]=2&ids[]=3

答案 1 :(得分:0)

首先,您应该添加指向卡片/我的回复的链接

EmberApp.ApplicationSerializer = DS.RESTSerializer.extend({
  normalizePayload: function(type, payload) {
    if (type.toString() === 'EmberApp.Card') {
      payload.links = { 'operations': '/cards/me/operations' };
      return { card: payload };
    } else if (type.toString() === 'EmberApp.Operation') {
      return { operations: payload };
    }
 }

});

然后在路线

EmberApp.CardOperationsRoute = Ember.Route.extend({
  model: function() {
    return this.controllerFor('sessions.new')
      .get('currentCard')
      .get('operations');
  }
});