1

I'll explain my problem with a quick example.

Parse.initialize("nlxy5xYYZQ1fLfFkzcyLHOifkie1dOU0ZSxoxw1w", "IRBJO7nyd1vQquhMvnyMd298ZVJ0qWg1AjxBY5nr");
var People = Parse.Object.extend("People");

var app = angular.module('app', []);

app.controller("MyCtrl", ["$scope", "PeopleService", function($scope, PeopleService){
  $scope.people = PeopleService.getPeople();
}]);

app.service("PeopleService", function(){
  var people = null;
  
  return {
    getPeople: function(){
      people = [];
      var queryObject = new Parse.Query(People);
      queryObject.find({
        success: function (results) {
          for (var i = 0; i < results.length; i++) {
            var result = results[i];
            people.push(result.get("name"));
          }
          return people;
        },
        error: function (error) {
          console.error("Error: " + error.code + " " + error.message);
        }
      });
    }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js">
</script>
<script src="https://www.parsecdn.com/js/parse-1.3.2.min.js"></script>
<div ng-app="app">
  <ul ng-controller="MyCtrl">
    <li ng-repeat="person in people">{{person}}</li>
  </ul>
</div>

Now, of course, the scope in the controller doesn't update when the data is returned by Parse. How can I make this service work properly?

3
  • 1
    You should take a look at $q and promises Commented Dec 10, 2014 at 12:13
  • Try parse-angular-patch to work with Parse's promise. Commented Dec 10, 2014 at 12:16
  • @james thank you. I'm reading about it now at https://docs.angularjs.org/api/ng/service/$q. I think this is the way. Meanwhile could you explain me how would you solve my problem using your solution. Just to have a reference on how promises should be used. Commented Dec 10, 2014 at 14:13

1 Answer 1

5

As @james stated before, using promises in $q service is what you are looking for. Here's how it could go with your example.

getPeople: function(){
          var deferred = $q.defer();
          people = [];
          var queryObject = new Parse.Query(People);
          queryObject.find({
            success: function (results) {
              for (var i = 0; i < results.length; i++) {
                var result = results[i];
                people.push(result.get("name"));
              }
              deferred.resolve(people);
            },
            error: function (error) {
              deferred.reject(error);
            }
          });
          return deferred.promise;
        }

This returns a promise which you can use like this

$scope.people = PeopleService.getPeople();

or

$scope.people = PeopleService.getPeople()
.then(function(people) {
    //modify data as necessary
    return people
  }, function(reason) {
    alert('Failed: ' + reason);
  }
);
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.