2013-08-30 56 views
0

我想配置一個使用ember-data連接並從一個api讀取數據的應用程序應用程序。 我的模式是:如何從Api讀取不同的root_node

App.Movie = DS.Model.extend 
    title: DS.attr 'string' 
    rating_average: DS.attr 'string' 
    short_plot: DS.attr 'string' 
    free: DS.attr 'boolean' 

我的API收益:

{ 
"pagination": { 
    "count":xx, 
    "page": x, 
    "total_pages": x 
}, 
"movies": [ 
    { 
     "id": xxxx, 
     "title": "xxx", 
     "rating_average": "x", 
     "short_plot": "xxxx", 
     "already_seen": x, 
.... 
.... 

當燼嘗試LO加載數據,它拋出:

Assertion failed: Your server returned a hash with the key **pagination** but you have no mapping for it 

灰燼並不指望在了 「分頁」 鍵JSON。我如何指定只嘗試從關鍵'電影'中讀取?

回答

1

儘管可以自定義ember-data來處理此問題,但應考慮使用ember-model。當你控制你的API並且可以確保遵循一系列約定時,ember-data的效果最好。如果不是這種情況,你會發現自己正在努力使你的API的燼數據工作,而且它不是爲這個用例而設計的。 ember-model是爲這個用例而設計的,儘管它對於你來說不那麼容易,但是定製起來會更容易。請參閱ember-model-introduction瞭解更多信息。

也就是說,要使用ember-data工作,您需要擴展其餘的適配器並自定義它的功能ajax

App.Store = DS.Store.extend({ 
    adapter: 'App.Adapter' 
}); 
App.Adapter = DS.RESTAdapter.extend({ 
    ajax: function(url, type, hash) { 
    var promise = this._super(url, type, hash); 
    return promise.then(function(json) { 
     delete json.pagination; 
     return json; 
    }); 
    } 
}); 

在我們呼籲其餘的適配器的Ajax功能,這個定製的適配器 - 見source - 它返回一個承諾。 promise的then()函數可用於從結果中去掉密鑰pagination。當然,這意味着pagination密鑰將從每個json響應中刪除。

相關問題