How can I create this JSON structure with JBuilder and Rails? -


tl;dr: how can use jbuilder create json looks this?

[   {}, // new pagination state   [{}, ...] // array of json objects ] 

longer version:

i trying pagination working backbone.js , backbone-pageable https://github.com/wyuenho/backbone-pageable .

backbone-pageable requires json returned formatted such it's array of 2 objects. first object regular object containing pagination control information. second object should array of actual data formatted backbone expect. these expectations hard coded backbone-pageable's parse methods (source)

it seems weird have array of un-like items use case seems acceptable.

for json i've used rabl in past science i'm trying use jbuilder , i've gotten far...

json structure (wrong):

{   "current_page": 1,   "total_pages": 6,   ...,    "entries": [     { "id": 131 },     ...   ] } 

using code:

json.current_page @posts.current_page ...  json.entries @posts |post|   json.extract! post, :id... end 

closer, still wrong :/

thank you

can try this:

json.array! [0,1] |index|   if index == 0     json.current_page @posts.current_page     ...    else     json.entries @posts |post|       json.extract! post, :id...     end   end end 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -