Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ember.js 余烬有很多_Ember.js_Ember Data - Fatal编程技术网

Ember.js 余烬有很多

Ember.js 余烬有很多,ember.js,ember-data,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 } }

我有一个模型

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”
然后我想找到操作抛出url/cards/me/operations
回应


如何从这个.controllerFor('*').get('currentCard')

您实际上需要在卡响应中以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

首先,您应该添加来自cards/me的响应链接

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');
  }
});

您应该调用
store.find
在routes
model()
-hook中,您应该能够通过
this.get('content.operations')访问它。然后(function(operations){})
在控制器中。不确定这是否有效,因为您在请求ID为“me”的记录时返回了另一个ID…但是如果我的api没有向我发送操作ID,并且对于操作,我有url/cards/:ID/operations?如果您无法遵循余烬数据需要有效负载结构的方式,您将需要一个自定义适配器。以thsi项目为例
EmberApp.CardOperationsRoute = Ember.Route.extend({
  model: function() {
    return this.controllerFor('sessions.new')
      .get('currentCard')
      .get('operations');
  }
});