Instead of focusing on a model event, we can focus on the review life cycle. For this purpose, Marionette makes the onBeforeDestroy callback available on Marionette.View (which expands with all kinds of Marionette). In ItemView, you define a callback like this
onBeforeDestroy: function () { $('#myElement').animate({ "height" : "0", 1000 }); }
This is an important disclaimer here. Since $.animate is an asynchronous function, it is possible that the view may be deleted before the $.animate transition completes. So, we have to make changes to our onBeforeDestroy .
onBeforeDestroy: function () { var animationDelay = 1000ms; this.remove = _.delay(_.bind(this.remove, this), animationDelay ); this.$el.animate({ "height" : "0", animationDelay }); }
Essentially, here we set the View.remove() method to run after an animation run, ensuring that when this.remove is called, it will be called asynchronously after the animation run. You can also do this with Promises, I suppose, but this is a bit expensive.
source share