2014-09-18 36 views
0

我使用rocket_pants寶石打造後端API https://github.com/Sutto/rocket_pants自定義適配器以支持RocketPant使用Rails

它有特定的格式輸出數據:

{ 
    "response":[ 
     {"id":1,"title":"Object Title","description":"Object Description"}, 
     {"id":1,"title":"Object Title","description":"Object Description"} ], 
    "count":2, 
    "pagination": { 
      "previous":null, 
      "next":null, 
      "current":1, 
      "per_page":30, 
      "count":2, 
      "pages":1} 
} 

我使用Batman.RailsStorage堅持模型。但是像MyApp.Model.get('all')這樣的動作在後端工作正常,但他們實際上不解析和加載模型對象。

你能指導我如何配置StorageAdapter或寫一個新的來處理這種類型的數據格式。

我們將不勝感激您的幫助/指導/支持。

感謝

回答

1

使用@rmosolgo的回答中提到的相同方法,我也構建了paginator。

class MyApp.RocketPantsPaginator extends Batman.ModelPaginator 
    totalCountKey: "pagination.count" 

    loadItemsForOffsetAndLimit: (offset, limit) -> 
    params = @paramsForOffsetAndLimit(offset, limit) 
    params[k] = v for k,v of @params 
    @model.load params, (err, records, env) => 
     if err? 
     @markAsFinishedLoading() 
     @fire('error', err) 
     else 
     response = new Batman.Object(env.response) 
     @set('totalCount', response.get(@totalCountKey)); 
     @updateCache(@offsetFromParams(params), @limitFromParams(params), records)