Hello all,
I am building a simple learning app and I am having difficulties with the
way I want to implement a factory. The app is using cordovasSQLite plugin.
(Leaning IonicFramework as well)
What I initially wanted the 'all' method to do was to return all the
projects in the database. Although when I have something like this
var db = window.openDatabase("TodoDevDB", "1.0", "Todo Dev Database",
10000);
.factory('Projects', function($cordovaSQLite) {
return {
all : function(){
var allProjects = [];
var query = "SELECT id, name FROM projects";
$cordovaSQLite.execute(db, query).then(function(result) {
if (result.rows.length > 0) {
for(var i = 0 ; i < result.rows.length; i++){
console.log(result.rows.item(i));
allProjects.push(result.rows.item(i));
}
} else {
console.log("no rows");
}
}, function(error) {
console.log("Error: " , error);
});
console.log(allProjects);
return allProjects;
}
}
})
Every time 'allProjects' is logged or returned, it is empty. I believe that
is because the a promise is asynchronous. So instead of returning the data
I in the factory, I had the 'all' method return just the promise, like
below.
.factory('Projects', function($cordovaSQLite) {
return {
all: function() {
var query = "SELECT id, title from projects";
return $cordovaSQLite.execute(db, query);
}
})
Then in the controller handle all the data that was returned from the
service call. Yet, this doesn't seem correct. Is there a way I can return
the data from a promise inside the factory?
Hopefully I have explained my question well.
Any help is greatly appreciated.
Thank you
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.