lit*_*ito 13 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()
});
Run Code Online (Sandbox Code Playgroud)
我正在逐个映射我的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.
}
});
Run Code Online (Sandbox Code Playgroud)
谢谢!
contacts其他答案是正确的,但我认为每次从to添加项目时都没有必要启动摘要循环$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.
}
});
Run Code Online (Sandbox Code Playgroud)
选项 1) 和 2) 将对应于类似于以下的模板
<div ng-repeat="cont in addContList">{{ cont.get('name') }}</div>
Run Code Online (Sandbox Code Playgroud)
而选项 3) 可以像这样使用
<div ng-repeat="cont in addContList">{{ cont.name }}</div>
Run Code Online (Sandbox Code Playgroud)