View Full Version : bufferedManagedListener for Element.js

8 Jan 2007, 9:43 AM
So while coding today i need a buffered Listener but I wanted it to be managed so that way i didn't have to deal with the event handeling of different browsers.

I came up with this patch Element.js

* Appends a managed listener to this element that is buffered. If the event is triggered more than once
* in the specified time-frame, only the last one actually fires.
* @param {String} eventName The type of event to listen for
* @param {Function} handler The method the event invokes
* @param {Object} scope (optional) The scope (this object) for the handler
* @param {Number} millis (optional) The number of milliseconds to buffer (defaults to 250)
* @return {Function} The wrapped function that was created (can be used to remove the listener)
bufferedManagedListener : function(eventName, fn, scope, millis){
var task = new YAHOO.ext.util.DelayedTask();
scope = scope || this;
var newFn = function(){
task.delay(millis || 250, fn, scope, Array.prototype.slice.call(arguments, 0));
this.mon(eventName, newFn);
return newFn;

I have created a patch file for this if Jack likes it i can send it his way to just put in on the svn server Just let me know.

8 Jan 2007, 10:34 AM
Looks good to me. I will add it in. Thanks!

8 Jan 2007, 10:43 AM
I've added it, but I called it "bon".

8 Jan 2007, 11:17 AM
I've added it, but I called it "bon".

Ok so bufferedManagedListener will not be there? Just want to make sure so i can edit my code accordingly.



8 Jan 2007, 11:29 AM
It was just too long for me. I would never use it. ;)

By the way, you may have trouble in IE accessing the event object using a buffer. IE has only one event object (window.event) and if another event fires while the timeout is executing the e will be invalid.

8 Jan 2007, 11:35 AM
Sounds good to me. I am just using it for the keyup event. do you think that would cause problems? I have not started testing my latest round of changes in IE yet. Let me know what you think. I'll let you know what i find out later also.

8 Jan 2007, 12:09 PM
It was just too long for me. I would never use it. ;)

I would personally vote for adding both versions, as "bon" is not too meaningful to the inunitiated and would be hard to find for someone scanning a property list looking for familiar terms like "buffered". I think it should be just like "mon" and "addManagedListener" to be consistent. My .02 :)

8 Jan 2007, 12:46 PM
a delayed listener is not good when using multiple text boxes as i learned. so i just changed it to using mon with the event of change which works well but now with the bon it's easier to add a monitored buffered listenter to a single text box or other items.

13 Sep 2007, 12:40 AM
For future searchers looking to only receive a single event of a given type in a period:

this is now more easily accomplished by passing a 'buffer' option to addEventListener (http://extjs.com/deploy/ext/docs/output/Ext.EventManager.html#addListener) (on)


MyTextArea.el.on('keyup',SaveTextArea,this,{buffer:500} );

(this gives me an 'autosave' for a textarea, when typing has stopped for 500 milliseconds).

HTH someone

Matt S