0

I'm trying to get the following findTimelineEntries function inside an Angular controller executing after saveInterview finishes:

$scope.saveInterview = function() {
        $scope.interviewForm.$save({employeeId: $scope.employeeId}, function() {
            $scope.findTimelineEntries();
        });
    };

The save action adds or edits data that also is part of the timeline entries and therefore I want the updated timeline entries to be shown. First I tried changing it to this:

$scope.saveInterview = function() {
        var functionReturned = $scope.interviewForm.$save({employeeId: $scope.employeeId});
        if (functionReturned) {
            $scope.findTimelineEntries();
        }
    };

Later to this:

$scope.saveInterview = function() {
        $scope.interviewForm.$save({employeeId: $scope.employeeId});
    };

    $scope.saveInterview.done(function(result) {
        $scope.findTimelineEntries();
    });

And finaly I found some info about promises so I tried this:

    $scope.saveInterview = function() {
        $scope.interviewForm.$save({employeeId: $scope.employeeId});
    };

    var promise = $scope.saveInterview();
    promise.done(function() {
        $scope.findTimelineEntries();
    });

But somehow the fact that it does work this way according to http://nurkiewicz.blogspot.nl/2013/03/promises-and-deferred-objects-in-jquery.html, doesn't mean that I can use the same method on those $scope.someFuntcion = function() functions :-S

3
  • is $scope.interviewForm.$save a $resource? Commented May 18, 2013 at 1:56
  • Nope, that's the scope which function you see explained at this page: docs.angularjs.org/guide/dev_guide.mvc.understanding_controller Commented May 21, 2013 at 10:41
  • $save is not mentioned at that link. Are you just looking for an example using angular promises? Commented May 21, 2013 at 12:16

1 Answer 1

1

Here is a sample using promises. First you'll need to include $q to your controller.

$scope.saveInterview = function() {
   var d = $q.defer();
   // do something that probably has a callback. 
   $scope.interviewForm.$save({employeeId: $scope.employeeId}).then(function(data) {
      d.resolve(data);  // assuming data is something you want to return. It could be true or anything you want.
   });


   return d.promise;

}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.