PDA

View Full Version : Is this even possible create element and listening and fire event?



Cyberangel67
27 Aug 2010, 5:22 PM
I am looking at creating something like this in ExtJS, it seems that jQuery has a very nice handy feature here.

http://www.bennadel.com/blog/1998-Using-jQuery-To-Bind-And-Trigger-Event-Handlers-On-Detached-DOM-Nodes.htm

When I write the code like this, I get a weird error.

uncaught exception: Error listening for "ping". Element "undefined" doesn't exist.

The code is


freeNode = new Ext.Element('div');
freeNode.on('ping', function() {alert('You pinged me');});


Anyone have any ideas on how to achieve this?

Animal
27 Aug 2010, 11:42 PM
But do you really NEED an element?

If you just want to fire events from SOMETHING, instantiate an Observable.



var myComponent = new Ext.util.Observable();
myComponent.on('ping', function(){alert("ping");})
myComponent.fireEvent("ping");

Cyberangel67
28 Aug 2010, 12:25 AM
Thanks Animal, I sort of figured I could do that. I was curious on more of whether one could create an element that was detached from the DOM and still be able to apply events to it.

Come to think of it I am not even sure of a good case that one would need too.

Animal
28 Aug 2010, 12:37 AM
There is no use case.

That blog post is just ignoring the heffalump in the room, that jQuery has no Observable base class!