Issue
I am using Parse data, and I want to return the variable ParseUserArray. But just after it passing by the success promise. I was wondering how can I do that.
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;
};
Solution
You can use promise
for this - $q
service
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
});
}]);
Answered By - ajai Jothi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.