In my AngularJS app I got two factories called "editInfo" and "pinVerify". What I'm trying to do is to have the editInfo factory call the pinVerify service with a callback function like this pinVerify.sendPin(this.saveInfo)
and then save that method inside the pinVerify factory for when the user has inputted the pin and presses a button calling pinVerify.pinEntered()
The problem with this is that the callback-method will get copied into the pinVerify object and will be unable to reference properties in the editInfo factory.
So... naturally I tried using $q.defer()
inside the pinVerify factory instead, and resolve it in pinVerify.pinEntered()
when the user enters the pin sent out by SMS.
This works perfectly fine, and I can inside the editInfo factory do this instead of using callbacks:
pinVerify.sendPin().then(function() {
this.saveInfo();
}
And the saveInfo method will detect if the pin was correct and hide the modal to enter it. The problem arises when the pin is not correct.
The promise has already been resolved, so another click on the button calling pinVerify.pinEntered
will do nothing basically. I can not resolve it twice.
So now I'm kind of back where I started, and seriously considering heavy drinking while on the job...
Any suggestions for how I can accomplish this kind of relationship between two factories? Suggestions on good beers/vodkas works too I guess.
While my first try before even going into the $q promise looked like this and didn't work - pinVerify.sendPin(self.saveAddress);
I just now solved it myself using a closure-method as the callback and then inside it calling the actual method using a previously declared var self = this;
like this
pinVerify.sendPin(function() {
self.saveAddress();
});