13

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!

3 Answers 3

3

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>
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer! I'm using $scope.addContList into a ng-repeat so I need an array of objects similar to this: [{name: "myname1"},{name: "myname2"},{name: "myname3"}]. If I use your solution I'll end up having ParseObject that I can't use (or at least not that easy). If you look at the code comments I have: $scope.addContList = contacts.map(function(obj) {return {name: obj.get("name")};}); I'm remapping the object for that same reason.
I see. So what was wrong with your contacts.map(...) solution?
I think is verbose and I'm looking for a better option. Thanks!
You can try something like contacts.map(function (c) { return c.toJSON() })). This will of course copy all the properties of the Parse.Object into the plain JS object, but you can use whichever ones you need ("name" for example).
Also as you can see I need to use the obj.get() from Parse.com to get the actual value.
|
2
+25

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.

2 Comments

Thanks for your answer! I'm using $scope.addContList into a ng-repeat so I need an array of objects similar to this: [{name: "myname1"},{name: "myname2"},{name: "myname3"}]. If I use your solution I'll end up having ParseObject that I can't use (or at least not that easy). If you look at the code comments I have: $scope.addContList = contacts.map(function(obj) {return {name: obj.get("name")};}); I'm remapping the object for that same reason.
Can you please give provide a sample on plunker or jsfiddle?
2

Change:

$scope.addContList = contacts[i];

to

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

1 Comment

Thanks for your answer! I'm using $scope.addContList into a ng-repeat so I need an array of objects similar to this: [{name: "myname1"},{name: "myname2"},{name: "myname3"}]. If I use your solution I'll end up having ParseObject that I can't use (or at least not that easy). If you look at the code comments I have: $scope.addContList = contacts.map(function(obj) {return {name: obj.get("name")};}); I'm remapping the object for that same reason.

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.