我已经将这两个问题(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无法设置未定义的属性“存储”类型错误:无法设置未定义的属性“存储”

最佳答案

当您请求一条记录时,它使用不同的序列化程序终结点,并且期望数据采用不同的格式。期望的格式是:

{
  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'
    }
  ]
}

09-26 22:14
查看更多