1

api/profile/:accountname/:profilename

I am trying to use my api in a restful way using $resource. My data model is: each account has a list of profiles, and those profiles should have GET, POST, PUT. However, I generally don't get just one profile, rather all the profiles if I GET without profilename, then the whole account is returned.

var re = $resource('/api/profile/:accountname/:profilename', { accountname : '@accoutname', profilename : '@profilename' });
var r =  re.get({ accountname: 'leo', profilename: 'qwe' }, function (data ) {
        //change some properties on r.
        //ideally, I should be able to put r is a $scope, and have two-way binding working and $save the changes to the server.
        r.$save({ accountname: 'leo', profilename: 'qwe' });
    });

The problem with this code is that there is no data posted to the server. It was already frustrating enough that I have to pass the parameters again in the $save method. Shouldn't it remember its parameters, since I already specified it in the get method from which I got the object I am calling $save on. If there is a better way to do it please let me know. When I inspect data in the callback, it is what I expect from the server, but now I just need to be able to post whatever itself is to the server when call $save. The official doc is so minimal that it is not obvious to know what each method takes and returns, it needs a lot of improvement.

EDIT: I solved the no data posted to server problem, and it is in the comments. The mystery still remains that I have to supply parameters in my $save method while the official doc doesn't.

EDIT2: Mystery solved, see comments.

21
  • My $resource uses .save() not .$save(). Additionally, it returns a $promise. Commented Aug 5, 2014 at 21:30
  • @Tony is the documentation wrong? It uses the exact pattern leo has including $save: docs.angularjs.org/api/ngResource/service/$resource Commented Aug 5, 2014 at 21:31
  • Ahah! Look here: stackoverflow.com/a/18434501/413397 Commented Aug 5, 2014 at 21:36
  • I also just noticed that the save call is executing on r, which isn't a $resource. Commented Aug 5, 2014 at 21:41
  • 1
    @Tony: r is the same instance as data. Commented Aug 5, 2014 at 21:55

0

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.