0

I've got an AngularJS resource implementation, which (in principle) works fine. Now the special circumstances are:

  • one of the fields (attributes) of the resource is the "last changed timestamp"
  • if it arrives at the server in an updating request, it is ignored. The sever sets the "last changed timestamp" always automatically
  • the updating methods on the server are all implemented in a way, that the response is empty (rather than containing the modified entity)

So under these circumstances, to get the "last changed timestamp" after an update operation from the server, I always have to do a get immediately following the update.

myEntity.$update( function(){ myEntity.$get(); } );

Now the question is: Does AngularJS offer a way to automatically chain actions:

  • to define in the MyEntity definition, that it needs to always do a $get after the $update.
  • and then, in application code just call

    myEntitty.$update();
    
2
  • 1
    Have you considered adding your own methods to myEntity? stackoverflow.com/questions/12076309/… Commented Apr 28, 2014 at 17:43
  • Thanks! not sure, why I didnt think about that myself ;) Commented Apr 29, 2014 at 11:05

1 Answer 1

1

Thanks to @marck for pushing me in the right direction. The solution is basically

    var MyEntity = $resource( ...,
        { 
            get: {...},
            update: {...}
        }
    );


    // params is passed to $update.
    // after successful $update, a parameterless $get is called
    // success is called after successful $get
    // error is called if $update OR $get failed
    // returns the promise of the $update
    MyEntity.prototype.$updateAndRefresh = function(params, success, error){
        var item = this;
        return item.$update(params, function(){
            item.$get(success, error);
        },
        error);
    }
Sign up to request clarification or add additional context in comments.

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.