2014-09-01 52 views
2

我有型號灰燼獲得的hasMany

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

比我做

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

響應

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

讓顯卡型號,但沒有進行操作,並將其設置到控制器託 「currentCard」
然後我想找到操作扔url/cards/m E /操作
響應

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

我怎樣才能做到這一點從this.controllerFor( '*')。獲得( 'currentCard')......?

+0

你應該調用'''store.find'''在路線'''模型()'''-hook,和你應該能夠通過'''this.get('content.operations')。然後(函數(操作){})在控制器內部訪問它。但不知道,如果這樣做,因爲當你要求ID爲'我'的記錄時,你返回另一個ID ... – thriqon 2014-09-01 06:32:18

回答

0

首先,你應該從卡鏈接添加到響應/我

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

});

在路線

然後

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

實際需要來回報您的操作作爲ID的列表中你這樣的卡迴應:

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

這將使你能夠做到這一點的另一條路線:

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

或本在你的卡片控制器中:

this.get('content.operations'); 

這將執行以下調用你的A PI:

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

但是,如果我的api不會給我操作ID和操作,我有url/cards /:id /操作? – ButuzGOL 2014-09-02 07:11:17

+0

如果您無法遵循Ember Data所需的有效負載結構,您將需要一個自定義適配器。看看thsi項目的例子https://github.com/toranb/ember-data-django-rest-adapter – abarax 2014-09-03 07:30:14