我正在使用Parse数据,但是交易是,我想返回变量ParseUserArray,但只是在它通过成功promisse之后。我想知道我该怎么做。
var UserWs = angular.module('UserWs', []);
UserWs.service('UserWsService', ['parseInit', function(parseInit){
var service = this;
this.getUserAtParse = function(id){
var user = Parse.Object.extend("User");
var query = new Parse.Query(user);
var parseUserArray = [];
query.find({
success: function(anUser) {
for (var i = 0; i < anUser.length; i++) {
var newUser = new User(anUser[i]);
parseUserArray.push(newUser);
}
console.log(parseUserArray);
}
});
var User =function(anUser){
this.id = anUser.id;
this.name = anUser.get("name");
this.email = anUser.get("username");
this.company = anUser.get("company");
}
return parseUserArray;
};
提前感谢:)
答案 0 :(得分:2)
您可以使用promise
- $q
服务
var UserWs = angular.module('UserWs', []);
UserWs.service('UserWsService', ['$q', 'parseInit', function($q, parseInit) {
var service = this;
this.getUserAtParse = function(id) {
var defer = $q.defer();
var user = Parse.Object.extend("User");
var query = new Parse.Query(user);
var parseUserArray = [];
query.find({
success: function(anUser) {
for (var i = 0; i < anUser.length; i++) {
var newUser = new User(anUser[i]);
parseUserArray.push(newUser);
}
console.log(parseUserArray);
defer.resolve(parseUserArray);
}
});
var User = function(anUser) {
this.id = anUser.id;
this.name = anUser.get("name");
this.email = anUser.get("username");
this.company = anUser.get("company");
}
return defer.promise;
}
}]);
UserWs.controller('sampleCtrl', ['$scope', 'UserWsService', function($scope, UserWsService) {
UserWsService.getUserAtParse(SOME_ID).then(function(resultArray) {
//logic here
});
}]);