0

I'm trying to figure out the best way to do an update of my model after doing an update.

So let's say I have my resource which I call to do an update, and then I attempt to do another query on the success function. I get into the success function and my query is successfuly done, but I can't seem to figure out how to get my result from the query back into my model's scope. Perhaps I'm taking the wrong approach for this?

Here's my example:

var myResource = new MyResource();
myResource.$update({
    resourceId : resourceId
   }, function (u) {
     u.$query({
    resourceId : resourceId
   }, function (result){
       $scope.mymodel = result;
   })
});

So in my above example, I see my query successfully being called. But I never seem to get into my callback function on the query. But maybe going this route to do a query after an update is the wrong path? If I'm understanding correctly, the update (put) is asynchronous. So if I want to update my model after an update, I need to use a callback function or some other method?

DavidB
  • 2,064
  • 3
  • 17
  • 17

1 Answers1

0

Why do you need to do a query after the update? If your backend was more RESTful, updates would respond with the updated value.

Then your code would be like this:

var myResourceId = 123;
var myResource = new MyResource();

// get will instantly return an empty object. Angular will "hydrate" it when the 
// response is returned, automagically.
$scope.mymodel = myResource.get({resourceId: myResourceId}); 

// Change something on the model
$scope.mymodel.someProperty = "monkeys";
$scope.mymodel.$update();  // Does a POST with the someProperty set to the new value
Odinodin
  • 2,147
  • 3
  • 25
  • 43