Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing function arguments to $scope.$apply?

I'm using $scope.$apply for callbacks, specifically with Stripe. Currently I have some code like

var stripeCallback = function (status, response) {
    if (!response.error) {
        // do something
    } else {
        // do something else
    }
};

$scope.submit = function () {
    $scope.errorMessage = 'Processing...';
    $scope.buttonDisabled = true;
    // can't use bindings for some reason
    var myForm = $('#paymentform');
    Stripe.createToken(myForm, function (status, response) {
        $scope.$apply(stripeCallback);
    });
};

The problem with this is that I can't get any arguments to stripeCallback, namely response. Is there any way I can pass function arguments to $scope.$apply? I haven't been able to find any reference to this is the documentation.

like image 566
jclancy Avatar asked Jun 07 '13 19:06

jclancy


2 Answers

Wrap stripeCallback into an anonymous function:

var stripeCallback = function (status, response) {
    ....
};

$scope.submit = function () {
    $scope.errorMessage = 'Processing...';
    $scope.buttonDisabled = true;
    // can't use bindings for some reason
    var myForm = $('#paymentform');
    Stripe.createToken(myForm, function (status, response) {
        $scope.$apply(function() {
            stripeCallback(status, response);
        });
    });
};
like image 98
TheHippo Avatar answered Nov 02 '22 17:11

TheHippo


You can also just call $scope.$apply() after calling your callback:

Stripe.createToken(myForm, function (status, response) {
    stripeCallback(status, response);
    $scope.$apply();
});
like image 5
Mark Rajcok Avatar answered Nov 02 '22 17:11

Mark Rajcok