Say I have a template which displays a view based on a property:
{{#if App.contentsAreVisible}}
{{view ToggleContents}}
{{/if}}
This area is toggled by any number of other parts of the UI by setting App.set("contentsAreVisible", [true/false]);
This all works fine.
However, I now want to animate when the view is toggled. Hooking into didInsertElement works to animate showing the area, but I can’t do the same in willDestroyElement because the element is removed as soon as that function returns, before the animation has a chance to run.
App.ToggleContents = Ember.View.extend({
// this works fine
didInsertElement: function(){
this.$().hide().show("slow");
},
// this doesn't work
willDestroyElement: function(){
this.$().hide("slow", function(){
// animation is complete, notify that the element can be removed
});
}
});
Is there any way to tell the view to defer removing the element from the DOM until the animation is complete?
Here’s a fiddle with an interactive example: http://jsfiddle.net/rlivsey/RxxPU/
You could create a
hidefunction on your view which removes the view when the callback is finished, see http://jsfiddle.net/7EuSC/Handlebars:
JavaScript: