oitozero / ngSweetAlert

AngularJS wrapper for SweetAlert
MIT License
616 stars 160 forks source link

Callback function not firing #59

Open jortiz56 opened 8 years ago

jortiz56 commented 8 years ago

I have a problem with the function call back, suddenly stop working. I don't have errors in the console.

arkadiuszneuman commented 8 years ago

Are you using sweetalert or sweetalert2?

jortiz56 commented 8 years ago

Hi sweetalert2

simultsop commented 8 years ago

Hey there (using sweetalert),

the problem occurs at my end too..

Currently I am dealing with an access matter, probably, as of I try to fire a swal with timer in an angular click directive SweetAlert.swal({ title: "Good job", text: 'Yada yada', timer: 3000, showConfirmButton: false, allowOutsideClick: true, allowEscapeKey: true }, function() { $scope.$parent.listLiveLines = true; }); fortunately the $scope.$parent.listLiveLines gets the true value but the swal is not dissapearing, you have to click outside or escape it..

jortiz56 commented 8 years ago

try this:

'use strict';

angular.module('AnyName.ngSweetAlert2', [])
.factory('sweetAlert', ['$rootScope', '$window', function ($rootScope, $window) {

    var swal = $window.swal;

    var self = function (arg1, arg2) {
        $rootScope.$evalAsync(function () {
            if (typeof (arg2) === 'function') {
                var functionAsStr = String(arg2).replace(/\s/g, '');
                var functionHandlesCancel = functionAsStr.substring(0, 9) === "function(" && functionAsStr.substring(9, 10) !== ")";

                var doneFn;
                if (functionHandlesCancel) {
                    doneFn = function (isConfirm) {
                        $rootScope.$evalAsync(function () {
                            arg2(isConfirm);
                        });
                    };
                } else {
                    doneFn = function () {
                        $rootScope.$evalAsync(function () {
                            arg2();
                        });
                    };
                }
                swal(arg1).then(doneFn);
            } else {
                swal(arg1, arg2);
            }
        });
    };

    var props = {
        swal: self,
        success: function (title, message) {
            $rootScope.$evalAsync(function () {
                swal(title, message, 'success');
            });
        },
        error: function (title, message) {
            $rootScope.$evalAsync(function () {
                swal(title, message, 'error');
            });
        },
        warning: function (title, message) {
            $rootScope.$evalAsync(function () {
                swal(title, message, 'warning');
            });
        },
        info: function (title, message) {
            $rootScope.$evalAsync(function () {
                swal(title, message, 'info');
            });
        },
        showInputError: function (message) {
            $rootScope.$evalAsync(function () {
                swal.showInputError(message);
            });
        },
        close: function () {
            $rootScope.$evalAsync(function () {
                swal.close();
            });
        }
    };

    angular.extend(self, props);

    return self;
}]);