1

all actions for a AngularJS $resource are added as $customAction methods to the Resource so that I can invoke them as methods on resource instances, e.g.:

var User = $resource('/user/:userId', {userId:'@id'});
User.get({userId:123}, function(user) {
  user.abc = true;
  user.$save();
});

$save is the save action and can be called on the user instance.

I now defined the following custom action, leading to a $attributes method on every instance of user:

var User = $resource('/user/:userId', {userId:'@id'}, {
  attributes: { method: 'GET', url: '/user/:userId/attributes', isArray: true }
});

If I now try to invoke this method, I get the error Object #<Resource> has no method 'push':

User.get({userId:123}, function(user) {
  var attrs = user.$attributes();    // error
  // ...
});

Did I get the concept of custom actions wrong?

2 Answers 2

1

It seems that you should use params instead of url

var User = $resource('/user/:userId/:customAction', {userId:'@id'}, {
  attributes: { method: 'GET', params: { userId:'@id', customAction: 'attributes'}, isArray: true }
});
Sign up to request clarification or add additional context in comments.

Comments

0

The eventual solution for me was to switch to Restangular. It supports nested resources using a chaining syntax so that I can to the following:

Restangular.one("user", 123).all("attributes").get()

Comments

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.