我正在使用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.
}
});
谢谢!
答案 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]);
你应该好好去。您之前的代码重新分配addContList
为contacts
数组中的每个元素,而不是将元素添加到其中。因此,在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];