I have a form which I want to be sent only if the user provided a valid access key ($scope.access_key
) - and each key can be used only once.
In my controller I have a following method:
$scope.verifyAccess = function() {
var ref = new Firebase(FBURL+'/keys');
var sync = $firebase(ref);
$scope.keys = sync.$asArray();
var success = false;
$scope.keys.$loaded( function(KEYS) {
for( var i = 0; i < KEYS.length; i++ ) {
var e = KEYS[i];
console.log(e.key + " " + e.used);
if( e.key === $scope.access_key && e.used == false ) {
e.used = true;
$scope.keys.$save(i);
success = true;
break;
}
}
if( success ) {
console.log("success");
return true;
} else {
console.log("failure");
return false;
}
});
}
And then I use it like this:
$scope.addSurvey = function() {
if( $scope.verifyAccess() ) {
// do something
alert("OK");
}
};
When I call $scope.addSurvey()
I see "success" in my log (and the database is modified properly), but alert("OK")
doesn't fire.
If I don't use $loaded
, method returns immediately and doesn't wait for the data to be loaded and processed. On the other hand, if I use $loaded
, it doesn't seem to return anything at all.
What am I doing wrong?