0

我尝试通过 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”?

这是JsBin

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

4

1 回答 1

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

于 2014-07-31T04:34:29.947 回答