I am working on a form that should only be submitted if the user provides a valid access key ($scope.access_key
) - and each key can only be used once.
Within my controller, I have the 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 call it like this:
$scope.addSurvey = function() {
if( $scope.verifyAccess() ) {
// do something
alert("OK");
}
};
Even though "success" is logged in my console (indicating database modifications), the alert("OK")
statement does not trigger when calling $scope.addSurvey()
.
It seems that without using $loaded
, the method returns immediately without waiting for data processing. However, when utilizing $loaded
, there appears to be no returned value at all.
What could be the issue with my implementation?