| Page 1 of 1 | [ 4 posts ] |
|
What is the correct way, if any, to call a function when a node is destroyed? This, my naive approach to the problem, does not work:
Code: var b = Y.one('#button'); var b_destroy = function() { alert('destroy'); } b.after('destroy', b_destroy); b.on('click', function(e) { e.preventDefault(); e.target.remove(true); }); In this toy example, I want to call the function b_destroy() when the button node is destroyed. In this case I could obviously just call b_destroy() from the handler I already have on the click event for the button. In the real usage case I've got a widget that creates and manages some content, and one or more plugins may decorate that content by e.g. adding wrapper divs around the widget-created content. I want to be able to have the plugins set up listeners on the nodes they decorate, so when the widget destroys them, the plugins can handle cleaning up any wrappers they may have added. |
Marco AsbreukYUI Contributor
|
Hi Stephen,
I don't quite understand the relation with your question and the purpose. Is it all about your plugin? In that case: a plugin always needs to take care of its own 'mess'. So you need to create the plugin in a way that it will clean up the wrappers as soon as it is unplugged (that is manually by calling node.unplug('yourplugin'), or automaticly by destroying the hostnode. If you destroy your hostnode, it will automaticly unplug any plugins. And while you took care of cleaning up, the wrappers should disappear. The plugin you should define with the destructor method like this: Code: destructor : function() { // cleaning up wrappers }, That is all you have to do. I suggest you also take care of the widget's destructor. This one should unplug all plugins. Code: destructor : function() { this.unplug('yourplugin'); }, Kind regards, Marco. |
|
I understand what you're saying, but in my usage case there isn't a widget and plugin instance for each of the nodes that is created/destroyed during the lifespan of the widget. The widget creates hundreds or thousands of nodes during its life. Each contains some content and has a couple controls. Depending on what kind of content is in the node, one or more plugins will add additonal controls, content, or markup.
The way I'm currently approaching it is to have a widget method for adding and removing each node. The plugins use afterHostMethod to set up listeners on the add and remove methods, and add or cleanup their markup as needed. This works, but I had originally tried to listen on the add method, and then have the plugin set up a listener for the destroy event on any node it decorates. This doesn't appear to work, although it is apparently syntactically valid. This leads me to believe I don't understand how listeners on the destroy method are supposed to work (if at all), and that's why I asked. |
Juan Ignacio DopazoYUI Contributor
|
Nodes don't fire a destroy event, but they do unplug all plugins they had plugged. So node.remove(true) will run all the plugins' destroy method automatically. In your plugins, define a destroy method, clean up everything there and you'll be good to go.
|
| Page 1 of 1 | [ 4 posts ] |
| You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group