Emberjs - 直接调用嵌套资源的URL

时间:2014-05-29 21:52:38

标签: javascript ember.js

我已将这两个问题(How to pass model in Nested routes - emberjsEmbedded data from RestApi)合并为一个JsBin示例:http://jsbin.com/OxIDiVU/544

如果您浏览客户,它可以正常工作 - >信息 - >联系,但如果直接联系客户的联系方式,它将会中断,例如:http://jsbin.com/OxIDiVU/544#/customers/3/contact

加载路线时出错:customer.contact无法设置属性' store'未定义的TypeError:无法设置属性' store'未定义的

1 个答案:

答案 0 :(得分:1)

当您对单个记录执行请求时,它使用不同的序列化程序端点,并期望数据采用不同的格式。它期望的格式是:

{
  customer: {
    id: 1,
    currency:1
  },
  currencies: [
    {
      id:1,
      prop: 'foo'
    }
  ]
}

串行器中的端点为extractSingle。随意提取相似的extractArray部分并分享这些部分。

假装你的有效载荷是:

  {
    customer:{
      id:3,
      name:"Joue",
      currency:{
        id:5,
        iso_code:"BDT"
      }
    } 
  }

您的extractSingle

  extractSingle: function(store, type, payload, id) {
    var customer = payload.customer,
        currencies = [];


    var currency = customer.currency;
    delete customer.currency;
    if(currency){
      currencies.push(currency);
      customer.currency = currency.id;
    }

    payload = { customer:customer, currencies: currencies };

    return this._super(store, type, payload, id);
  }

以下是示例,其中包含对客户3的回复

http://jsbin.com/OxIDiVU/545#/customers/3/contact

你的属性名称应该在模型中匹配,根名称(这里的货币)应该是它的记录类型的复数形式。

{
  customer: {
    id: 1,
    default_currency:1
  },
  currencies: [
    {
      id:1,
      prop: 'foo'
    }
  ]
}