jeudi 21 janvier 2016

Make an RSVP hash that returns a set of arrays a model for route

I have an RSVP hash that looks up two sets of data from my API

return Ember.RSVP.hash({
  event: this.store.findRecord('event',params.event_url),
  items: Ember.$.ajax({
    type: 'GET',
    dataType: 'json',
    url: `http://myendpoint/api/cart/event/${this.get('localStorage').getItem('user').active_auction}`,
    data: {
      'user': this.get('localStorage').getItem('user').id,
      'key': ENV.APP.api_key
    },
  }).then((response)=>{
    console.log(response);
    return response;
  }, (reason)=>{
    console.log(reason);
    this.get('notification').notify({message: "An error occurred, please try again.",success: false});
  })
}).then(function(hash){
    return hash;
},function(reason){
    this.get('notification').notify({message: "There was an error loading the cart",success:false});
});

The endpoint I'm hitting for items returns something that looks like this

{
  "items": [
    {
      "item_id": "11477",
      "item_name": "Item Two",
      "bid_amt": "96"
    },
    {
      "item_id": "11478",
      "item_name": "Item Three",
      "bid_amt": "200"
    }
  ],
  "donations": [],
  "tickets": [],
  "raffle_purchases": [
    {
      "name": "Test Raffle",
      "description": "",
      "image": "4zMncOFYZW-cropped.png",
      "raffle_purchase_id": "1258",
      "raffle_ticket_total": "24"
    }
  ]
}

Each array is going to represent it's data in different ways, and the keys will be different for similar things such as name or even ID. Now I could probably go through the API and return this differently but I figured there might be something I can do Ember side to work with this.

In my template how might I be able to iterate over each array separately? Currently I'm trying it like this

{{#each model.items.items key="item_id" as |item|}}
    ...
{{/each}}

But that isn't displaying anything and I don't see anything in the console to direct me to how I might be able to make this work.

My first thought is maybe I can make this a call through ember data and use a serializer and adapter to handle this?




Aucun commentaire:

Enregistrer un commentaire