How Custom Events Will Save the Universe
-
Upload
andrew-dupont -
Category
Technology
-
view
14.583 -
download
0
Transcript of How Custom Events Will Save the Universe
![Page 1: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/1.jpg)
How CUSTOM EVENTS
will save the universe
![Page 2: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/2.jpg)
Law of Demeter
![Page 3: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/3.jpg)
“Each unit should have only limited knowledge
about other units.”
![Page 4: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/4.jpg)
(we break this ruleall the time)
![Page 5: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/5.jpg)
the Law of Demeter is about removing
unnecessary coupling
![Page 6: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/6.jpg)
custom events can helpdecouple your code
![Page 7: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/7.jpg)
Broadcaster and receiverdon’t have to knowabout one another…
![Page 8: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/8.jpg)
… so there’s far less to mockin your unit tests
![Page 9: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/9.jpg)
YOU DO HAVEUNIT TESTS, RIGHT?
![Page 10: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/10.jpg)
How do custom events work?
![Page 11: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/11.jpg)
![Page 12: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/12.jpg)
$(document).observe('custom:event', function(event) { var customData = event.memo; // stuff}); $('troz').fire('custom:event', { foo: "bar" });
![Page 13: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/13.jpg)
![Page 14: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/14.jpg)
$(document).bind('customevent', function(event, customData) { // stuff}); $('#troz').trigger('customevent', [{ foo: "bar" }]);
![Page 15: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/15.jpg)
![Page 16: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/16.jpg)
$(document).addEvent('customevent', function(event, customData) { // stuff}); $('troz').fireEvent('customevent', { foo: "bar" });
![Page 17: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/17.jpg)
BUT WHY?
![Page 18: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/18.jpg)
Makes testing easier
(maybe you’ll do it now)
Why?
![Page 19: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/19.jpg)
Rapid prototyping
Why?
![Page 20: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/20.jpg)
More versatile than callbacks
Why?
![Page 21: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/21.jpg)
Can be bubbled/canceled
Why?
![Page 22: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/22.jpg)
Can handle exceptions properly(in theory)
Why?
![Page 23: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/23.jpg)
(quoth Dean Edwards)
![Page 24: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/24.jpg)
In Prototype,exceptions raised in one handler
won’t affect another handler
![Page 25: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/25.jpg)
(is this a big deal? smart people disagree)
![Page 26: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/26.jpg)
CASESTUDIES
![Page 27: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/27.jpg)
#1
Case Studies
![Page 28: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/28.jpg)
(script.aculo.us 2.0)
Case Studies
![Page 29: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/29.jpg)
…as a “metronome” for effects
scripty2 uses custom events…
![Page 30: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/30.jpg)
window.setTimeout(function() { document.fire('effect:heartbeat');}, 0);
![Page 31: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/31.jpg)
document.observe('effect:heartbeat', advanceEffectByOneFrame);
![Page 32: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/32.jpg)
Seems pointless…
…until you need to debug
![Page 33: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/33.jpg)
Step through an animationframe by frame
document.observe('keydown', function(event) { if (event.keyCode === Event.KEY_RIGHT) { document.fire('effect:heartbeat'); }});
![Page 34: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/34.jpg)
…to pass messagesbetween UI widgets
scripty2 uses custom events…
![Page 35: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/35.jpg)
![Page 36: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/36.jpg)
![Page 37: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/37.jpg)
S2.UI.Menu
(used by other S2.UI components)
![Page 38: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/38.jpg)
var menu = new S2.UI.Menu();menu.addChoice("Foo");menu.addChoice("Bar");someElement.insert(menu);menu.open();
![Page 39: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/39.jpg)
menu.observe('ui:menu:selected', function(event) { console.log('user clicked on:', event.memo.element);});
S2.UI widgets act like elements when needed, so...
![Page 40: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/40.jpg)
Easy to usein any context
![Page 41: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/41.jpg)
Button with menu
![Page 42: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/42.jpg)
ASIDE:
Custom events are cancelable
![Page 43: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/43.jpg)
var event = $('troz').fire('custom:event');if (!event.stopped) performSomeDefaultAction();
![Page 44: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/44.jpg)
document.observe('ui:menu:before:open', function(event) { event.stop();});
(prevent all menus from appearing)
![Page 45: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/45.jpg)
…as hooks for debugging
scripty2 uses custom events…
![Page 46: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/46.jpg)
“Why aren’t theseeffects queueing
like I expect?”
![Page 47: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/47.jpg)
document.observe('effect:dequeued', function(event) { var queue = event.memo; console.log("Effects in queue:", queue.size());});
![Page 48: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/48.jpg)
You get debuggingFOR FREE
![Page 49: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/49.jpg)
#2
Case Studies
![Page 50: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/50.jpg)
Mouse wheel
Case Studies
![Page 51: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/51.jpg)
window.addEventListener('DOMMouseScroll', handler);window.onmousewheel = handler;
function handler(event) { var delta; if (event.wheelDelta) { delta = event.wheelDelta / 120; if (window.opera) delta = -delta; // (not a joke) } else if (event.detail) { delta = -event.detail / 3; }
// Do stuff with your stupid delta}
http://adomas.org/javascript-mouse-wheel/
![Page 52: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/52.jpg)
Instead, do this:
![Page 53: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/53.jpg)
window.addEventListener('DOMMouseScroll', handler);window.onmousewheel = handler;
function handler(event) { var delta; if (event.wheelDelta) { delta = event.wheelDelta / 120; if (window.opera) delta = -delta; // (not a joke) } else if (event.detail) { delta = -event.detail / 3; }
// Fire a custom event with the normalized delta. var result = event.target.fire('mouse:wheel', { delta: delta }); if (result.stopped) event.preventDefault();}
![Page 54: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/54.jpg)
See also:
hashchange
![Page 55: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/55.jpg)
#3
Case Studies
![Page 56: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/56.jpg)
User idle state
Case Studies
![Page 57: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/57.jpg)
credit: kangax
document.observe('state:idle', function() { turnOffBackgroundAjaxRequests();});
document.observe('state:active', function() { turnOnBackgroundAjaxRequests();});
http://perfectionkills.com/detect-idle-state-with-custom-events/
![Page 58: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/58.jpg)
#4
Case Studies
![Page 59: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/59.jpg)
Keyboard events
Case Studies
![Page 60: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/60.jpg)
(function() { var KEYS = {}; KEYS[Event.KEY_ESC] = "esc"; KEYS[Event.KEY_UP] = "up"; KEYS[Event.KEY_DOWN] = "down"; KEYS[Event.KEY_LEFT] = "left"; KEYS[Event.KEY_RIGHT] = "right"; // ... and so on
function handler(event) { if (event.keyCode && KEYS[event.keyCode]) { event.element().fire("key:" + KEYS[event.keyCode], event); } }
document.observe("keydown", handler);})();
![Page 61: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/61.jpg)
Then you’d be able to dosomething like this:
document.observe('key:left', function() { moveSlide(-1); });document.observe('key:right', function() { moveSlide( 1); });
![Page 62: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/62.jpg)
#5
Case Studies
![Page 63: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/63.jpg)
Data broadcasting
![Page 64: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/64.jpg)
Server-sent events are awesome……but not universally supported
![Page 65: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/65.jpg)
for browsers that support server-sent events
var eventSource = $('event_source');eventSource.observe('server-sent-event-name', function(event) { document.fire('data:received', event.data);});
![Page 66: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/66.jpg)
for browsers that don’t
new Ajax.Request('/legacy/polling', { onComplete: function(request) { document.fire('data:received', request.responseJSON); }});
![Page 67: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/67.jpg)
observer works with either approach
$(document).observe('data:received', function(event) { doStuffWithYourData(event.memo);});
![Page 68: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/68.jpg)
…and your unit tests can fire dummy data
function testThatSomethingHappened() { document.fire('data:received', FIXTURE_DATA); assertSomething();}
![Page 69: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/69.jpg)
FAQ
![Page 70: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/70.jpg)
“What if my eventsaren’t DOM-related?”
FAQ
![Page 71: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/71.jpg)
(meh)
![Page 72: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/72.jpg)
Use the DOM anyway, I say
![Page 73: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/73.jpg)
“Isn’t this overkill?”
FAQ
![Page 74: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/74.jpg)
Yes, sometimes
![Page 75: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/75.jpg)
“Aren’t events slow?”
FAQ
![Page 76: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/76.jpg)
NO
![Page 77: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/77.jpg)
Events aren’t slow;they’re synchronous
![Page 78: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/78.jpg)
Events are onlyas slow as the handlers
attached to them
![Page 79: How Custom Events Will Save the Universe](https://reader033.fdocuments.in/reader033/viewer/2022060204/559f765d1a28abdb718b4876/html5/thumbnails/79.jpg)
If performance is a concern, defer
window.setTimeout(function() { document.fire('costly:custom:event');}, 0);