Ember-data JSON结构

时间:2014-08-01 15:00:46

标签: ember.js ember-data

我在某些关系中有5个模型:

App.Service = DS.Model.extend({
  name: DS.attr('string'),
  service_prices: DS.hasMany('servicePrice')
});

App.ServicePrice = DS.Model.extend({
  unit_price: DS.attr('number'),
  qty_unit: DS.belongsTo('qtyUnit'),
  service: DS.belongsTo('service'),
  partner:DS.belongsTo('partner')
});

App.Partner = DS.Model.extend({
    "name": DS.attr('string')
});

App.QtyUnit = DS.Model.extend(Ember.Validations.Mixin, {
  name: DS.attr('string'),

});

App.Order = DS.Model.extend({
  service: DS.belongsTo('service'),
  unit_price: DS.attr('numeric'),
  qty_unit:DS.belongsTo('qtyUnit')
});

我尝试使用以下JSON加载订单:

var order = {
  "order" : {"id":1,"service":1,"qty_unit":4,"unit_price":10},
  "service":[{"id":1,"name":"ENG-GER","service_prices":[1,2]}],
  "servicePrices":[
    {"id":1,"qty_unit":4,"unit_price":3,"partner":1},
    {"id":2,"qty_unit":5,"unit_price":4,"partner":1}
  ],
  "qtyUnits":[
    {"id":4,"name":"character"},
    {"id":5,"name":"word"},
    {"id":6,"name":"sentence"}   
  ],
  "partner":[
    {"id":1,"name":"Jessie Bains"}  
  ]  

};

但我得到以下错误:

加载路径时出错:TypeError:无法读取未定义的属性'deserialize'

我的Json结构错误吗?

这是JsBin:

http://jsbin.com/finahuna/12/edit

1 个答案:

答案 0 :(得分:1)

请求记录时,json中的关系应为复数(servicespartners

var order = {
  "order" : {"id":1,"service":1,"qty_unit":4,"unit_price":10},
  "services":[{"id":1,"name":"ENG-GER","service_prices":[1,2]}],
  "servicePrices":[
    {"id":1,"qty_unit":4,"unit_price":3,"partner":1},
    {"id":2,"qty_unit":5,"unit_price":4,"partner":1}
  ],
  "qtyUnits":[
    {"id":4,"name":"character"},
    {"id":5,"name":"word"} 
  ],
  "partners":[
    {"id":1,"name":"Jessie Bains"}  
  ]  

};

此外,你的jsbin并不工作,因为:

范围内存在

servicesqtyUnits(可能是您正在调试)

return Ember.RSVP.hash({
  order:store.find('order',1),
  services: store.all('service'),
  qtyUnits: store.all('qtyUnit')
});

如果您的控制器有对象支持,则需要延长ObjectController而不是Controller

App.IndexController = Ember.ObjectController.extend({

});

示例:http://jsbin.com/wimoz/1/edit