ButuzGOL
ButuzGOL

Reputation: 1243

Ember get hasMany

I have Model

EmberApp.Card = DS.Model.extend({
  balance: DS.attr('number'),
  operations: DS.hasMany('operation', { async: true })
});

Than I make

self.store.find('card', 'me')

response

{ 
  "card": {
    "id": "53620486168e3e581cb5851a",
    "balance": 20
  }
}

getting card Model but without operations and setting it to controller prop "currentCard"
Then I want to find operations throw url /cards/me/operations
response

{"operation": [{ "id": 1, "type": 1 }] }

How can I do it from this.controllerFor('*').get('currentCard')... ?

Upvotes: 2

Views: 1814

Answers (2)

ButuzGOL
ButuzGOL

Reputation: 1243

First of all you should add links to response from cards/me

EmberApp.ApplicationSerializer = DS.RESTSerializer.extend({
  normalizePayload: function(type, payload) {
    if (type.toString() === 'EmberApp.Card') {
      payload.links = { 'operations': '/cards/me/operations' };
      return { card: payload };
    } else if (type.toString() === 'EmberApp.Operation') {
      return { operations: payload };
    }
 }

});

Then in route

EmberApp.CardOperationsRoute = Ember.Route.extend({
  model: function() {
    return this.controllerFor('sessions.new')
      .get('currentCard')
      .get('operations');
  }
});

Upvotes: 0

abarax
abarax

Reputation: 6299

You actually need to return your operations as a list of ID's in your card response like this:

{ 
  "card": {
    "id": "53620486168e3e581cb5851a",
    "balance": 20,
    "operations": [1, 2, 3]
  }
}

This will enable you to do this in another route:

this.modelFor('card').get('operations');

or this in your cards controller:

this.get('content.operations');

This will execute the following call to your API:

/api/operations?ids[]=1&ids[]=2&ids[]=3

Upvotes: 1

Related Questions