user3568719
user3568719

Reputation: 1056

Emberjs - Calling directly nested resource's URL

I have merged together from this two problem (How to pass model in Nested routes - emberjs and Embedded data from RestApi) a JsBin example: http://jsbin.com/OxIDiVU/544

It works fine if you navigate customers-> info -> contact, but it will break if one calls directly a customer's contact eg.:http://jsbin.com/OxIDiVU/544#/customers/3/contact

Error while loading route: customer.contact Cannot set property 'store' of undefined TypeError: Cannot set property 'store' of undefined

Upvotes: 1

Views: 35

Answers (1)

Kingpin2k
Kingpin2k

Reputation: 47367

When you do a request for a single record, it uses a different serializer endpoint and expects the data in a different format. The format it expects is:

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

And the endpoint in the serializer is extractSingle. Feel free to extract out the portions of extractArray that are similar and share those.

Pretending your payload is:

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

Your extractSingle would be

  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);
  }

Here's the example, with a response for customer 3

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

your property name should match inside the model, and the root name (currencies here) should be the plural version of the type of record it is.

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

Upvotes: 1

Related Questions