-
-
Save subtleGradient/385763 to your computer and use it in GitHub Desktop.
| var myListener; | |
| // Make a Listener instance | |
| myListener = element.listen().relay("a[href^=#]").listen('click').tell(function(event){ event.stop() }); | |
| myListener = Butt.listen({ | |
| listen: 'kick', | |
| relay: "a[href^=#]", | |
| tell: function(event){ event.stop() }, | |
| default | |
| }); | |
| myListener = myListener.target.listen('keyup:key(esc):relay(a[href])', function(){}); | |
| myListener = new Listener; | |
| // Do stuff with your Listener instance | |
| // The target and currentTarget are the element until the event actually happens | |
| element === myListener.element === myListener.target === myListener.currentTarget; | |
| myListener.listen('click, mousedown, keydown'); | |
| myListener.relay('#nav a[href]'); | |
| // Listener.addHandler === Listener.tell | |
| myListener.addHandler('kick', myBoot, yourButt); // The best imho ;) | |
| myListener.addHandler('kick'.bind(myBoot, yourButt))); // Requires String.prototype.bind | |
| myListener.addHandler(Function.bind('kick', myBoot, yourButt)); | |
| myListener.addHandler(myBoot.kick, myBoot, yourButt); | |
| myListener.addHandler(myBoot.kick.bind(myBoot, yourButt))); | |
| myListener.addHandler(Function.bind(myBoot.kick, myBoot, yourButt)); | |
| myListener.removeHandler(myBoot.kick); | |
| var myBoot = new myBoot; | |
| var yourButt = new Butt; | |
| myListener.preventDefault(); | |
| function clickmyBoot(event){ | |
| // The Listener instance can also be used as a proxy to the Event instance | |
| myListener === event; | |
| // The current handler property is the fn that is currently handling the event | |
| myListener.handler === clickmyBoot; | |
| // `this` is the currentTarget element that is handling the event at the moment | |
| myListener.currentTarget === this; | |
| myListener.preventDefault(); | |
| myListener.allowDefault(); | |
| } | |
| var Butt = function(){/*Please don't kick this :-!*/}; | |
| var myBoot = function(){/*This can kick you!*/}; | |
| myBoot.prototype.kick = function(shoe){ | |
| alert("You kick butt!"); | |
| } | |
| // Function.bind(verb, noun, arguments) |
Currently, Event instances aren't very useful. You can use them for your current event but after it's done it's useless.
A listener instance that has 0 or many events could be much more useful.
You could event subclass Listener to create a custom multi-event handler for things like drop-down menus, drag-n-drop and touch stuff. Anything that involves multiple related events like how touchstart, touchcancel, gesturemove, etc… are all inter-related for even relatively simple touch gestures like clicking a button and virtual-scrolling and handling not clicking the button when you are scrolling instead.
Assuming we have a variable called myList that fires some events and a Class called ListItem that implements a mixin called Method…
var isListItem = ListItem.getMethod('match'); // returns the bound ListItem.match method
var swipe = new ListenerGestureSwipe; // Create a new listener
swipe
.listenTo(myList) // Tell the listener to listen for swipes on myList
.relay(isListItem); // and act on them relative to anything that isListItem returns true for
swipe.addHandler( // Tell the listener to do something when the swipe action happens.
Method.invoke('delete') // Pass in a 'delete' method that will be bound to the specific relayed listItem that got swiped
);
this assumes that listener.relay accepts matcher functions. Then you could pass in a Slick.match that was partially applied with your selector. Or you could pass in your own function that matches stuff based on some custom logic.
The delegation code in Listener would handle the bubbling and matching with your matcher.
Once it finds a match it'll save it to a property of itself (e.g. listener.relayTarget) for use inside your handlers.
Unlike a temporary Event instance that information will still be usable outside of your handlers for whatever.
One possible advantage could be performance.
Currently when an event actually happens (e.g. mousemove) we have to take the real event object, wrap it and then setup the fancy magic to let it do awesome things.
If we instantiated a listener instance when adding the event we wouldn't have to do it when the event actually happens.
When the event actually happens we would take the listener, add the real event object as a property and that's it. Super-fast.
That single listener instance would be reused for multiple logically similar events. All its methods could be written to work with the current event like so:
this.currentEvent.preventDefault()