如何在QueryParse.com之后将所有对象加载到数组中

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

本文关键字:对象 加载 数组 QueryParse com 之后      更新时间:2023-09-26

我使用Parse.com作为后端,在Query之后,我如何用Parse对象中的所有数据填充数组?如何避免重新映射?示例:

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

我正在一个接一个地映射我的Parse对象的属性:名称:obj.get("name")等等。有更好的方法吗?

    $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. 我应该使用Undercore库吗?这是唯一的方法吗
  2. 我见过一些人使用PFQuery,但我不知道那是什么,PFQuery对此更好吗

谢谢!

其他答案都是正确的,但我认为没有必要每次将项目从contacts添加到$scope.addContList时都启动摘要循环。这样的东西应该足够了:

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

选项1)和2)将对应于类似的模板

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

而选项3)可以像一样使用

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

如果更改

$scope.addContList = contacts[i];

至:

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

你应该很乐意去。您以前的代码将addContList重新分配为contacts数组中的每个元素,而不是将元素添加到其中。因此,在for循环结束时,$scope.addContList将是contacts数组中的最后一个联系人。

更改:

$scope.addContList = contacts[i];

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