我尝试通过mockjax为ember数据建模ajax调用。
我的模特:
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')
});
App.QtyUnit = DS.Model.extend(Ember.Validations.Mixin, {
name: DS.attr('string'),
});
App.Order = DS.Model.extend({
service: DS.belongsTo('service'),
qty_unit:DS.belongsTo('qtyUnit'),
});
我尝试通过order
加载mockjax
记录。 (按下按钮。)根据呼叫后的控制台
MOCK GET: /orders/1
Object {url: "/orders/1", type: "GET", isLocal: false, global: true, processData: true…}
Ember数据尝试第二次调用
GET http://run.jsbin.com/services/%3CApp.Service:ember518:1%3E 404 (Not Found)
首先,我不会忘记为什么第二次打电话?服务是按我的订单JSON
加载的var order = {
"order" : {"id":1,"service":1,"qty_unit":4},
"service":[{"id":1,"name":"ENG-GER","service_prices":[1,2]}],
"servicePrices":[
{"id":1,"qty_unit":4,"unit_price":3},
{"id":2,"qty_unit":5,"unit_price":4}
],
"qtyUnits":[
{"id":4,"name":"character"},
{"id":5,"name":"word"},
{"id":6,"name":"sentence"}
]
};
为什么尝试使用ember-data调用记录App.Service:ember518:1
而不是其id" 1"?
答案 0 :(得分:2)
问题是你的setQtyUnits方法。您正在传递服务模型,而不仅仅是模拟端点所期望的id。在这种情况下,ember518是服务模型实例的ember生成的名称,它被传递而不是id。修改后的方法是 -
setQtyUnits:function(){
var service_id = this.get('model.order.service.id');
if (service_id !== null)
{
var self = this;
//find returns a promise
this.store.find('service',service_id).then(function(service){
//on success
var servicePrices = service.get('service_prices');
var qtyUnits = servicePrices.mapBy('qty_unit');
console.log(qtyUnits);
self.set('qtyUnits', qtyUnits);
});
} else {
this.set('qtyUnits', null);
}
}.observes('model.order.service')
这是经过修改的bin - http://jsbin.com/finahuna/4/edit