我正在使用rocket_pants
gem来构建后端API https://github.com/Sutto/rocket_pants
它具有输出数据的特定格式:
{
"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
或编写新的处理此类数据格式吗?
答案 0 :(得分:1)
您可以尝试覆盖collectionJsonNamespace
method (defined on Batman.RestStorage
)。
我看到从{HTTP}响应中获取记录是used after a readAll
operation。
例如:
class MyApp.RocketPantsStorage extends Batman.RailsStorage
collectionJsonNamespace: -> "response"
然后在你的模特中
#= require ../rocket_pants_storage
# the storage adapter must be available at load time
class MyApp.SomeModel
@persist MyApp.RocketPantsStorage
这有用吗?
答案 1 :(得分: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)