Question

I'm trying to post the scalar value 0 with angular resource, e.g.

myResource.save({}, 0, onSuccess, onError};

This gives a HTTP 400 error for a POST attempt with empty body.

Looking into the angular sources I see that my post data ultimately becomes the argument to the constructor of Resource, which looks like

  function Resource(value){
    shallowClearAndCopy(value || {}, this);
  }

So it seems all falsy values will be replaced by an empty object and thus cannot be posted. Is this by design (and then: which part of the documentation did I miss?) or a bug?

Was it helpful?

Solution

The point of $resource is best illustrated when the instance methods are used instead of the class methods. Take this example from the docs:

 var CreditCard = $resource('/user/:userId/card/:cardId',
  {userId:123, cardId:'@id'}, {
   charge: {method:'POST', params:{charge:true}}
  });
 ...
 var newCard = new CreditCard({number:'0123'});
 newCard.name = "Mike Smith";
 newCard.$save();

Essentially newCard is just a plain object representing a model, but enriched with some functionality. $resource provides a layer of abstraction. It's an implementation of the Active Record Pattern.

That said, if you want to post a single scalar value, then chances are that you are dealing with a service rather than a resource. In that case it's somewhat obvious that $resource (sic!) isn't the right choice.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top