
Defines the :once
pseudo event for classes that have implemented the Events class as well as the definePseudos
method for adding your own.
Like Element.Event.Pseudos for Elements, you can use pseudos for Class Events.
An event pseudo looks the same as a CSS pseudo: event:pseudo(value)
.
event
is the eventname you used to use, and pseudo
is the name
of the pseudo.
The event will only fire once.
var database = new Class({ Implements: Events, connect: function(){ this.fireEvent('connect'); } }); var db = new database(); db.addEvent('connect:once', function(){ alert('i am connected'); }); db.connect(); // will alert 'i am connected' db.connect(); // nothing will happen
Makes sure the event is not fired more than once in a certain timespan. This is especially useful for events that might fire a lot, but it isn't really necessary to execute a heavy function, like sending requests.
The default timespan is 250 milliseconds.
myClass.addEvent('scroll:throttle', function(){ // Will only fire once every 250 ms }); myClass.fireEvent('scroll'); // is fired myClass.fireEvent('scroll'); // throttled myClass.fireEvent.delay(300, myClass, 'scroll'); // after 250 ms, so it's fired myClass.addEvent('resize:throttle(400)', function(){ // Will only fire once every 400 ms });
The event is only fired when the original event is not fired again in the given time. So when the first event is fired, and a second after 100 ms, the first event is cancelled and only the second is fired.
The default pausetime is 250 milliseconds.
myClass.addEvent('keydown:pause', function(){ // Default time is 250 ms }); myClass.fireEvent('keydown'); // The first event is cancelled myClass.fireEvent('keydown'); // this one will fired myClass.fireEvent.delay(1000, myClass, 'keydown'); // This one is after 250 ms, so the previous is not cancelled myClass.addEvent('keydown:pause(100)', function(){ // The pause time is now 100 ms. });
This function defines a new pseudo.
Events.definePseudo(name, fn);
once
will become click:once
fn(split, fn, args)
event:pseudo(value)
:
(
and )
:
and (
event:pseudo(value)
addEvent
method. So it is the 'fn' in myClass.addEvent('event:pseudo', fn)
This is how the :once pseudo is implemented
Events.definePseudo('once', function(split, fn, args){ fn.apply(this, args) this.removeEvent(split.original, fn); });
© Linux.ria.ua, 2008-2024 |