Professional Documents
Culture Documents
YUI Library: Event Utility & Custom Event v2.5: Simple Use Case: Adding Event Listeners Simple Use Case: Custom Event
YUI Library: Event Utility & Custom Event v2.5: Simple Use Case: Adding Event Listeners Simple Use Case: Custom Event
5
Simple Use Case: Adding Event Listeners Simple Use Case: Custom Event Event Utility Methods:
myEvt = new YAHOO.util.CustomEvent("my event"); addListener(…)
YAHOO.util.Event.addListener("myDiv", "click",
mySubscriber = function(type, args) { getCharCode(e)
fnCallback); getListeners(el [, type])
alert(args[0]); } //alerts the first argument
Adds the function fnCallback as a listener for the click event on an myEvt.subscribe(mySubscriber); getPageX(e)
HTML element whose id attribute is myDiv. myEvt.fire("hello world"); getPageY(e)
getRelatedTarget(e)
Creates a new Custom Event instance and a subscriber function; the subscriber getTarget(e)
Invocation (addListener) alerts the event's first argument, "hello world", when the event is fired. getTime(e)
getXY(e): returns array
YAHOO.util.Event.addListener(str | el ref | arr
Constructor (Custom Event) [pageX, pageY]
target[s], str event, fn callback[, obj onAvailable(s id || el ref, fn
associated object, b scope); YAHOO.util.CustomEvent(str event name[, obj scope object, callback, o obj, b scope)
b silent, int signature ]); onContentReady(s id || el
Arguments: ref, fn callback, o obj, b
(1) Element or elements: You may pass a single element or group of Arguments:
scope)
elements in an array; references may be id strings or direct element (1) Event name: A string identifying the event.
onDOMReady(s id || el ref,
(2) Scope object: The default scope in which subscribers will run; can be
references. fn callback, o obj, b
overridden in subscribe method.
(2) Event: A string indicating the event ('click', 'keypress', etc.). scope)
(3) Silent: If true, hides event's activity from Logger when in debug mode.
(3) Callback: The function to be called when the event fires. preventDefault(e)
(4) Argument signature: YAHOO.util.CustomEvent.LIST by default — all
(4) Associated object: Object to which your callback will have purgeElement(el [,
arguments passed to handler in a single array. .FLAT can be specified to
recurse, type])
access; often the callback's parent object. pass only the first argument. removeListener(…)
(5) Scope: Boolean — if true, the callback runs in the scope of the stopEvent(e): same as
associated object. Subscribing to a Custom Event preventDefault plus
stopPropagation
Event Utility Solutions myEvt.subscribe(fn callback[, obj associated object, b
scope]);
stopPropagation(e)