rest - How to handle pagination and count with angularjs resources? -


i have build angularjs client api outputting json this:

{   "count": 10,   "next": null,   "previous": "http://site.tld/api/items/?start=4"   "results": [     {       "url": "http://site.tld/api/items/1.json",       "title": "test",       "description": "",       "user": "http://site.tld/api/users/4.json",       "creation_datetime": "2013-05-08t14:31:43.428"     },     {       "url": "http://site.tld/api/items/2.json",       "title": "test2",       "description": "",       "user": "http://site.tld/api/users/1.json",       "creation_datetime": "2013-05-08t14:31:43.428"     },     {       "url": "http://site.tld/api/items/3.json",       "title": "test3",       "description": "",       "user": "http://site.tld/api/users/2.json",       "creation_datetime": "2013-05-08t14:31:43.428"     }   ] } 

how can make $resource maps ? if use isarray=false, i'll entire blob 1 object, usable reading, can't call .put() on it. if use isarray, doesn't work.

is there clean way this? or should go using $http?

you have couple of options. if can change server output add meta info (count, next, previous) header values instead of adding them in response body.

your second option transform response transformresponse. available $response configuration in angular v1.1.2 , later (the unstable branch):

var data = $resource('./data.json',{},{   list:{isarray:true,method:'get',     transformresponse: function (data, headers) {       return json.parse(data).results;     }} }); 

if don't want use unstable branch possible change $http $resource uses:

$http.defaults.transformresponse.push(function(data){   if(data && data.results){     return data.results;   } }); 

i've created plunker both examples: http://plnkr.co/edit/pmyotp0eo5k41z6zcxl4?p=preview

i'm not sure best approach passing on meta data rest of application (if need it). append first result, or add separate object - maybe not elegant, it'll job done.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -