Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load into an array all objects after Query Parse.com

I'm using Parse.com as my backend and after Query how can I fill an array with all the data inside the Parse object? how can I avoid re-mapping? example:

$scope.addContList = contacts.map(function(obj) { // re-map!!!!
   return {name: obj.get("name")}; // mapping object using obj.get()
});

I'm mapping my Parse object's properties one by one: name: obj.get("name"), etc. is there a better way?

    $scope.addContList = [];
    var ActivityContact = Parse.Object.extend("ActivityContact2");
    var query = new Parse.Query(ActivityContact);
    query.equalTo("activityId", $scope.objId);
    query.find({
        success: function(contacts) {
            console.log("Successfully retrieved " + contacts.length + " contact.");
                $scope.$apply(function() {
                    /*$scope.addContList = contacts.map(function(obj) {
                        return {name: obj.get("name")}; // mapping object using obj.get()
                    });*/
                    for (var i = 0; i < contacts.length; i++) {
                          $scope.addContList.push(contacts.ALL_PROPERTIES); // contacts.ALL_PROPERTIES does not exist, I'm looking a way to do that and avoid mapping?
                    }
                });
            console.log("--->>>"+JSON.stringify($scope.addContList, null, 4));

        },
        error: function(object, error) {
            // The object was not retrieved successfully.
            // error is a Parse.Error with an error code and message.
        }
    });
  1. Should I use Underscore library, is that the only way to go?
  2. I have seen some ppl using PFQuery but I don't know what is that, is PFQuery better for this?

Thanks!

like image 301
lito Avatar asked Nov 16 '15 15:11

lito


2 Answers

The other answers are correct, but I think it's unnecessary to launch a digest cycle every time you add an item from contacts to $scope.addContList. Something like this should be sufficient:

query.find({
  success: function (contacts) {
    $scope.apply(function () {
      // 1) shallow-copy the list of contacts...
      // (this is essentially what you are trying to do now)
      $scope.addContList = contacts.slice();

      // or 2) just assign the reference directly
      $scope.addContList = contacts;

      // or 3) transform the Parse.Object instances into
      // plain JavaScript objects
      $scope.addContList = contacts.map(function (c) {
          return c.toJSON();
      });
    });
  },
  error: function (object, error) {
    // The object was not retrieved successfully.
    // error is a Parse.Error with an error code and message.
  }
});

Options 1) and 2) will correspond to a template similar to

<div ng-repeat="cont in addContList">{{ cont.get('name') }}</div>

while option 3) can be used like

<div ng-repeat="cont in addContList">{{ cont.name }}</div>
like image 129
Igor Raush Avatar answered Sep 29 '22 14:09

Igor Raush


If you change

$scope.addContList = contacts[i];

to:

$scope.addContList.push(contacts[i]);

you should be good to go. Your previous code was re-assigning addContList to be each element in the contacts array, instead of adding the element to it. So at the end of your for loop, $scope.addContList would just be the last contact in your contacts array.

like image 20
Kevin Avatar answered Sep 29 '22 16:09

Kevin