1

I'm working on a pair of Angular functions that should change a value from false to true when the user clicks a button. The app tracks a user's favorite books; when a user creates a favorite, the default values for 'tracking' and 'finished' are set to false. When the user goes to update them to true using an ng-click, the new 'true' values are not patched to the database, and are logged in the console as still false. Any thoughts on what's missing from my functions?

$scope.trackFavorite = function(favorite) {
      var favoriteParams = {
        id: favorite.id,
        tracking: favorite.tracking,
        finished: favorite.finished
      };
      favorite.tracking = !favorite.tracking;
      $http.patch("/api/v1/favorites/"+favorite.id+".json", favoriteParams).success(function(response) {
        console.log("READING NOW");
        console.log(response);
      });
    };

    $scope.markFinished = function(favorite) {
      var favoriteParams2 = {
        id: favorite.id,
        finished: favorite.finished,
      };
      favorite.finished = !favorite.finished;
      console.log(favorite);
      $http.patch("/api/v1/favorites/"+favorite.id+".json", favoriteParams2).success(function(response){
        console.log("IS IT FINISHED");
        console.log(response);
      });
    };

Here's the ng-click snippets from the view, just in case:

<div>
 <button ng-class="{tracking: favorite.tracking}" ng-click="trackFavorite(favorite)">Reading Now</button>
</div>

<div>
 <button ng-class="{finished: favorite.finished}" ng-click="markFinished(favorite)">Finished</button>
</div>

Thanks a lot!

1
  • 1
    You should look to catch the error(s) coming back from your promise, which will give you context into why it's not correctly saving. Commented Oct 17, 2016 at 14:57

1 Answer 1

3

There could be a chance that you miss some http configuration. As it has been noticed here: patch request using angularjs.

It would also be a good idea to implement the error function in your controller and for example update the form according to the response, that you get back.

$scope.trackFavorite = function(favorite) {
      var favoriteParams = {
        id: favorite.id,
        tracking: favorite.tracking,
        finished: favorite.finished
      };

      $http.patch("/api/v1/favorites/"+favorite.id+".json", favoriteParams)
            .then(
               function(response) {
                  console.log("READING NOW");
                  console.log(response);
                  //update the UI according to the response
                  favorite.tracking = !favorite.tracking;
               },function(error){
                 //clean up when an error occurs
               });

    };
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.