12

I like the way the query() method returns an array of resources, which can be saved to the server again.
I am trying to use Angular against the Drupal RestWS module, which returns an object with several "meta" properties and a property called list where the actual data are stored. Is there please a way of telling the resource to take that array instead ?

Example : GET author.json returns :

first: "http://dgh/author?page=0"
last: "http://dgh/author?page=0"
list: [{id:1, type:author, uid:{uri:http://dgh/user/1, id:1, resource:user}, created:1367770006,…},…]
self: "http://dgh/author"

1 Answer 1

21

With the latest Angular version (1.1.2 or later), you can configure the resource with a transformResponse:

var MyResource = $resource(
    '/author.js',
    {},
    {
        'get': {
            method: 'GET',
            transformResponse: function (data) {return angular.fromJson(data).list},
            isArray: true //since your list property is an array
        }
    }
);
Sign up to request clarification or add additional context in comments.

4 Comments

this looks promising, but i cant get it to work, I keep getting TypeError: Object #<Resource> has no method 'push'. I think its a problem i usually have when an object is returned for a method that has isArray:true. BTW, is the new version documented somewhere ?
Hmm, ok. I'll see if I can fix it in a JSFiddle. The docs are here: code.angularjs.org/1.1.4/docs/api/ngResource.$resource , but the transformResponse isn't that well documented :(
I've changed the code a bit. This is a working example: jsfiddle.net/59nhp (with other data, so it's not the same transform function)
Set isArray:true, parse the data and return the array: list:{isArray:true,method:'get',transformResponse: function (data, headers) {return JSON.parse(data).list; }} - plnkr.co/edit/8tj1qqqzZ2KLWkDhT2mQ

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.