Singleton Theory with javascript and prototype
Feb1
I was searching for a way in javascript to handle classes dispatching events and listening for events. Generally javascript only has event methods for elements and not objects. Then I stumbbled onto http://www.truerwords.net where he has developed some event classes to mix-in to your own objects that give them the ability to listen and dispatch events. You must be using the prototype javascript library for these to work as that is what they were wrote with as well. Its pretty straight forward to use. Basically you include the event js file <script src="js/Events/event_mixins.js"></script> after this you will setup your classes as such
var classObj= Class.create(); Object.extend(classObj.prototype, { events: ['communicationTab', 'generalTab', 'scheduleTab', 'billingTab', 'financialAidTab', 'repositoryTab', 'sendEmail'],//events fire from this class initialize: function() { //add listeners for this object this.listenForEvent( classListeningForEventFrom, 'loadData', true ); }, //this makes the class able to dispatch events onLoadData: function(evt) {} }); //this makes the class able to listen for events Object.extend( classObj.prototype, , Event.Listener ); Object.extend( classObj.prototype, , Event.Publisher ); //To dispatch an event from this class its rather simple. All events that this class can dispatch should be set in your events array (more about this later). this.dispatchEvent('sendEmail', {emailTo:me@here.com});the second parameter in the dispatchEvent is data you want to pass to the receiving listener. For a class to be a listener you must put the listenForEvent on the object. This does not have to be in the constructor but if the class is always listening for these events I would recommend putting it. By default the listener class will try to call a method in the listening class the same name as the event being listened for except it adds “on” to the event. So if you listening for the event “loadData” then your method in the class should be “onLoadData” This is a fantastic approach to the issue with javascript and events. I don’t know why other libraries don’t incorporate something like this for their events. If you want to have a global listener so you don’t have to specify a specific class that your class is listening for I highly recommend looking at the Event Broker he has wrote.
Tagged as: dispatch, event, events, js, jscript, listener, Programming, prototype, prototype javascript, singleton theory1 Comment
1 Comment
Leave a comment
Categories
- Business (5)
- General (7)
- Programming (3)
- Javascript (2)
- prototypejs (2)
- PowerShell (1)
- Javascript (2)
- Software (2)
- Adobe (1)
- Dreamweaver (1)
- Microsoft (1)
- Outlook (1)
- Adobe (1)
- Typo3 (2)
What I'm Doing...
- http://bit.ly/cgCOcA a tribute to all the other nerds out there! 1 week ago
- http://bit.ly/9CFLpU wow incredible 2 weeks ago
- http://bit.ly/bWapF9 if you have cats you need to be aware of this. 2010-08-04
- More updates...
Posting tweet...

2:51 pm on May 11th, 2010
Interesting article i totally agree with the comments above. Keep us posting