Binding events to the object's prototype
Hi there,
I've just discovered something quite interesting: has it occurred to anyone that it's possible to bind events to the object's prototype and that you can fire those events from an instance method with the object as a target ?... have a look at this snippet:
- /* @Class */
- var MyClass = function () {...}
- /* @Members */
- MyClass.prototype.methodThatTriggersAnEvent = function () {
- // some code here
- $(this).trigger( 'myevent', [...] );
- }
- /* @Events */
- $(MyClass.prototype).bind({
- 'myevent' : function (e, params) {
- console.debug(e.target); // e.target = MyClass instance
- }
- });
- var myobj = new MyClass();
- myobj.methodThatTriggersAnEvent(); // will trigger 'myevent' with myobj as e.target
I regard this as a really interesting and useful feature. Anyway, I just wanted to share it in case someone was looking to implement some Observer patter or Event Pool system using jQuery bind/unbind/trigger methods.