2014-09-18 23 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)