392N/A This example will illustrate how to use the synthetic event creation
392N/A API. We'll create an `arrow` event that fires in response
392N/A to the user pressing the arrow keys (up, down, left, right) and adds a
457N/A `direction` property to the generated event.
392N/A <p>Subscribing to this new event will look like this:</p>
392N/A Support will also be added for delegation, allowing a single subscriber
392N/A from a node higher up the DOM tree, to listen for the new event
392N/A emanating from its descendant elements.
392N/A<div class="example yui3-skin-sam">
392N/A {{>synth-example-source}}
99N/A<h2>`on`, `fire`, and `detach`</h2>
392N/A The three interesting moments in the lifecycle of a DOM event subscription
0N/A <li>The event is subscribed to</li>
0N/A <li>The event is fired</li>
0N/A <li>The event is unsubscribed from</li>
527N/A <em>config</em> )`. Define the implementation logic for the
456N/A `on` and `detach` moments in the configuration.
527N/A Typically the condition triggering the event firing is set up in the
0N/A on: function (node, sub, notifier) {
457N/A // what happens when a subscription is made
0N/A detach: function (node, sub, notifier) {
508N/A // what happens when a subscription is removed
527N/A In the case of arrow handling, the trigger is simply a key event with a
889N/A `keyCode` between 37 and 40. There are a few browser quirks with arrow
889N/A handling that warrant listening to `keydown` for some browsers and
889N/A `keypress` for others, so we'll take care of that transparently for `arrow`
889N/A on: function (node, sub, notifier) {
0N/A // Webkit and IE repeat keydown when you hold down arrow keys.
0N/A // Opera links keypress to page scroll; others keydown.
0N/A // Firefox prevents page scroll via preventDefault() on either
457N/A // keydown or keypress.
0N/A // Bummer to sniff, but can't test the repeating behavior, and a
0N/A // feature test for the scrolling would more than double the code size.
0N/A // To make detaching the associated DOM event easy, store the detach
0N/A // handle from the DOM subscription on the synthethic subscription
0N/A // Only notify subscribers if one of the arrow keys was pressed
527N/A // Add the extra property
527N/A // Firing the notifier event executes the arrow subscribers
0N/A // Pass along the key event, which will be renamed "arrow"
0N/A detach: function (node, sub, notifier) {
0N/A // Detach the key event subscription using the stored detach handle
0N/A<h2>Add Delegation Support</h2>
527N/A Since the `arrow` event is simply a filtered `keydown` or `keypress` event,
0N/A no special handling needs to be done for delegate subscriptions. We will
0N/A extract the key event handler and use it for both `on("arrow", ...)` and
527N/A `delegate("arrow", ...)` subscriptions.
527N/A // Webkit and IE repeat keydown when you hold down arrow keys.
0N/A // Opera links keypress to page scroll; others keydown.
0N/A // Firefox prevents page scroll via preventDefault() on either
527N/A // keydown or keypress.
527N/A _keyHandler: function (e, notifier) {
527N/A on: function (node, sub, notifier) {
0N/A // Use the extended subscription signature to set the 'this' object
0N/A // in the callback and pass the notifier as a second parameter to
0N/A detach: function (node, sub, notifier) {
0N/A // Note the delegate handler receives a fourth parameter, the filter
0N/A // The filter could be either a string or a function.
0N/A delegate: function (node, sub, notifier, filter) {
0N/A filter, this, notifier);
0N/A // Delegate uses a separate detach function to facilitate undoing more
0N/A // complex wiring created in the delegate logic above. Not needed here.
0N/A detachDelegate: function (node, sub, notifier) {
0N/A Subscribe to the new event or detach the event as you would any other DOM
0N/A // to prevent page scrolling
527N/A // See full code listing to show the data set up
392N/Asubs =
Y.one('#demo').delegate('arrow', move, '.robot');
0N/A<h2>Bonus Step: to the Gallery!</h2>
527N/A Synthetic events are perfect candidates for Gallery modules. There are a
527N/A number already hosted there, and there are plenty of UI interaction
0N/A patterns that would benefit from being encapsulated in synthetic
527N/A The `arrow` event in this example is also
527N/A but with additional functionality. Check out
527N/A to see what you can do with synthetic events.
0N/A<h2>Full Code Listing</h2>
0N/A{{>synth-example-source}}