如何在Query Parse.com

时间:2015-11-16 15:52:11

标签: javascript angularjs parse-platform

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

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

我正在逐个映射我的Parse对象的属性:name: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. 我应该使用Underscore库,这是唯一的方法吗?
  2. 我见过一些使用PFQuery的人,但我不知道那是什么,PFQuery对此更好吗?
  3. 谢谢!

3 个答案:

答案 0 :(得分:3)

其他答案都是正确的,但我认为每次添加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>

答案 1 :(得分:2)

如果你改变了

$scope.addContList = contacts[i];

为:

$scope.addContList.push(contacts[i]);
你应该好好去。您之前的代码重新分配addContListcontacts数组中的每个元素,而不是将元素添加到其中。因此,在for循环结束时,$scope.addContList将成为contacts数组中的最后一个联系人。

答案 2 :(得分:2)

变化:

<div class="container">
    <div class="row">
        <div class="col-xs-4" id="chart_1">23</div>
        <div class="col-xs-4" id="chart_2">45</div>
        <div class="col-xs-4" id="chart_3">34</div>
    </div>
    <div class="row">
        <div class="col-xs-4" id="chart_3">34</div>
        <div class="col-xs-4" id="chart_1">23</div>
        <div class="col-xs-4" id="chart_2">45</div>
    </div>
</div>

$scope.addContList = contacts[i];