【问题标题】:Emberjs - Calling directly nested resource's URLEmberjs - 直接调用嵌套资源的 URL
【发布时间】:2014-05-29 21:52:38
【问题描述】:

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

【问题讨论】:

    标签: javascript ember.js


    【解决方案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'
        }
      ]
    }
    

    【讨论】:

    • kingpin2k:再次感谢您!如果我有“default_currency”而不是“currency”怎么办。我必须驼峰吗?在我的应用程序中一切都很好,只有当我有 default_currency = DS.belongsTo('currency') 时,才会出错。我找不到任何不同之处。概率。我也会为此设置一个 JsBin。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-18
    • 2013-03-02
    • 1970-01-01
    • 2014-05-14
    • 2013-05-06
    相关资源
    最近更新 更多