JavaScript Complete callback in AngularJS animation

I have an AngularJS animation...

app.animation('slide-show', function () {
    return {
        setup: function (element) {
        },
        start: function (element, done) {
            element.slideDown(400, done);
        }
    };
} );

app.animation('slide-hide', function () {
    return {
        setup: function (element) {
        },
        start: function (element, done) {
            element.slideUp(400, done);
        }
    };
} );

I would like to be able to supply an additional "complete" callback as a parameter to the start() methods so that it can be invoked/passed to the jQuery methods. Is this somehow possible through the animation directives?

Answer:1

I just had this question and this was how I achieved success:

I had the same setup for animation, but then I added an ng-animated property on the HTML element that I was animating, then I added the following:

app.animation('slide-show', function () {
    return {
        setup: function (element) {
        },
        start: function (element, done) {
            element.slideDown(400, function(){ 
                 var el = angular.element(element[0]);
                 el.scope().$eval(el.attr('ng-animated'));
                 done();
            });
        }
    };
} );

app.animation('slide-hide', function () {
    return {
        setup: function (element) {
        },
        start: function (element, done) {
            element.slideUp(400, function(){ 
                 var el = angular.element(element[0]);
                 el.scope().$eval(el.attr('ng-animated'));
                 done();
            });
        }
    };
} );

I hope this helps someone else!

Answer:2

You are already passing a "complete" callback (the second parameter to element.slideUp()), however you are not using your own but you simply call the done function Angular provides.

You must call that function eventually, but nothing keeps you from doing your own work beforehand.

app.animation('slide-hide', function () {
    return {
        setup: function (element) {
        },
        start: function (element, done) {
            element.slideUp(400, function () {
                // handle the end of the slideUp animation
                // ...

                // inform Angular that you're done
                done();
            });
        }
    };
});

You could generalize this, if you want:

function AnimationCallback(done, callback, callbackArgs) {
    return function () {
        if (typeof callback === "function") {
            // 'this' will be the DOM element that just finished animating
            callback.apply(this, callbackArgs);
        }
        done();
    }
}

and

app.animation('slide-hide', function () {
    return {
        setup: function (element) {
        },
        start: function (element, done) {
            element.slideUp(400, new AnimationCallback(done, yourCallback));
        }
    };
});

where yourCallback is any function you have defined and callbackArgs is an optional array of arguments that you'd like to pass to it.

Answer:3

I have a Bootstrap Modal that contains a form for updating or creating an entity (Company in my example). Right now my issue is that if I view an entity using the modal, it doesn't clear out the ...

I have a Bootstrap Modal that contains a form for updating or creating an entity (Company in my example). Right now my issue is that if I view an entity using the modal, it doesn't clear out the ...

I'm trying to write a benchmark, using benchmark.js, for some popular node.js unzip libs, mainly adm-zip, yauzl & unzip. However, I'm not sure if I've written the benchmark code correctly, because ...

I'm trying to write a benchmark, using benchmark.js, for some popular node.js unzip libs, mainly adm-zip, yauzl & unzip. However, I'm not sure if I've written the benchmark code correctly, because ...

I hope this question is not confusing or too complex. but here we go: I'm creating a custom google map that renders some US states as polygon overlays with the "google.map.Polygon' method. Everything ...

I hope this question is not confusing or too complex. but here we go: I'm creating a custom google map that renders some US states as polygon overlays with the "google.map.Polygon' method. Everything ...

I'm looking for a way to, from a backbone collection, retrieve some kind of array of one specific attribute. var SomeModel = Backbone.Model.extend({ defaults: function() { return { ...

I'm looking for a way to, from a backbone collection, retrieve some kind of array of one specific attribute. var SomeModel = Backbone.Model.extend({ defaults: function() { return { ...