From 2cead800b31d8d663dd7f7457350e4ab9485e2bb Mon Sep 17 00:00:00 2001 From: Aaron Date: Mon, 15 Oct 2012 14:12:31 -0400 Subject: [PATCH] updated formatting --- page/events/event-delegation.md | 27 +- page/events/event-extensions.md | 7 +- page/events/event-helpers.md | 10 +- page/events/events-to-elements.md | 16 +- page/events/inside-event-handling-function.md | 16 +- page/events/introduction-to-custom-events.md | 474 +++++++++--------- page/events/introduction-to-events.md | 106 ++-- page/events/triggering-event-handlers.md | 24 +- page/events/using_delegate_and_undelegate.md | 145 +++--- page/events/working_with_events_part_1.md | 90 ++-- 10 files changed, 457 insertions(+), 458 deletions(-) diff --git a/page/events/event-delegation.md b/page/events/event-delegation.md index 079a9d61..9ee1e538 100644 --- a/page/events/event-delegation.md +++ b/page/events/event-delegation.md @@ -26,20 +26,19 @@ method is available, and is the preferred method. ``` -//Event delegation using `$.fn.delegate` - - $('#myUnorderedList').delegate('li', 'click', function(e) { - var $myListItem = $(this); - // ... - }); +// Event delegation using `$.fn.delegate` +$('#myUnorderedList').delegate('li', 'click', function(e) { + var $myListItem = $(this); + // ... +}); ``` ``` -//Event delegation using `$.fn.live` - $('#myUnorderedList li').live('click', function(e) { - var $myListItem = $(this); - // ... - }); +// Event delegation using `$.fn.live` +$('#myUnorderedList li').live('click', function(e) { + var $myListItem = $(this); + // ... +}); ``` ### Unbinding Delegated Events @@ -49,7 +48,7 @@ use `$.fn.undelegate` for events connected with `$.fn.delegate`, and `$.fn.die` for events connected with `$.fn.live`. As with bind, you can optionally pass in the name of the bound function. ``` -//Unbinding delegated events - $('#myUnorderedList').undelegate('li', 'click'); - $('#myUnorderedList li').die('click'); +// Unbinding delegated events +$('#myUnorderedList').undelegate('li', 'click'); +$('#myUnorderedList li').die('click'); ``` diff --git a/page/events/event-extensions.md b/page/events/event-extensions.md index 473ef546..ab0bff92 100644 --- a/page/events/event-extensions.md +++ b/page/events/event-extensions.md @@ -94,7 +94,8 @@ When defined, these string properties specify that a special event should be han The behavior of these properties is easiest to see with an example. Assume a special event defined as follows: -```jQuery.event.special.pushy = { +``` +jQuery.event.special.pushy = { bindType: "click", delegateType: "click" }; @@ -112,10 +113,10 @@ So given the special event above, this code shows that a pushy isn't removed by ``` var $p = $("p"); $p.on("click", function( e ) { - $("body").append("I am a " + e.type + "!")); + $("body").append("I am a " + e.type + "!")); }); $p.on("pushy", function( e ) { - $("body").append("I am pushy but still a " + e.type + "!"); + $("body").append("I am pushy but still a " + e.type + "!"); }); $p.trigger("click"); // triggers both handlers $p.off("click"); diff --git a/page/events/event-helpers.md b/page/events/event-helpers.md index 69711d57..65db5621 100644 --- a/page/events/event-helpers.md +++ b/page/events/event-helpers.md @@ -16,10 +16,10 @@ Prior to jQuery 1.4, the `$.fn.hover` method required two functions. ``` -//The hover helper function - $('#menu li').hover(function() { - $(this).toggleClass('hover'); - }); +// The hover helper function +$('#menu li').hover(function() { + $(this).toggleClass('hover'); +}); ``` ### `$.fn.toggle` @@ -31,7 +31,7 @@ however, it will accept an unlimited number of functions. Be careful, though: providing a long list of functions can be difficult to debug. ``` -//The toggle helper function +// The toggle helper function $('p.expander').toggle( function() { $(this).prev().addClass('open'); diff --git a/page/events/events-to-elements.md b/page/events/events-to-elements.md index 1127b1f4..2fee928f 100644 --- a/page/events/events-to-elements.md +++ b/page/events/events-to-elements.md @@ -11,21 +11,21 @@ when you are working with custom events, or when you want to pass an object of multiple events and handlers. ``` -//Event binding using a convenience method +// Event binding using a convenience method $('p').click(function() { console.log('click'); }); ``` ``` -//Event biding using the `$.fn.bind` method +// Event biding using the `$.fn.bind` method $('p').bind('click', function() { console.log('click'); }); ``` ``` -//Event binding using the `$.fn.bind` method with data +// Event binding using the `$.fn.bind` method with data $('input').bind( 'click change', // bind to multiple events { foo : 'bar' }, // pass in data @@ -43,7 +43,7 @@ want no handler to run, or you may want a different handler to run. jQuery provides the `$.fn.one` method for this purpose. ``` -//Switching handlers using the `$.fn.one` method +// Switching handlers using the `$.fn.one` method $('p').one('click', function() { console.log('You just clicked this for the first time!'); $(this).click(function() { console.log('You have clicked this before!'); }); @@ -61,12 +61,12 @@ you can isolate the unbinding to that named function by passing it as the second argument. ``` -//Unbinding all click handlers on a selection +// Unbinding all click handlers on a selection $('p').unbind('click'); ``` ``` -//Unbinding a particular click handler, using a reference to the function +// Unbinding a particular click handler, using a reference to the function var foo = function() { console.log('foo'); }; var bar = function() { console.log('bar'); }; @@ -81,7 +81,7 @@ useful to namespace your events so you don't unintentionally disconnect events that you didn't or couldn't know about. ``` -//Namespacing events +// Namespacing events $('p').bind('click.myNamespace', function() { /* ... */ }); $('p').unbind('click.myNamespace'); $('p').unbind('.myNamespace'); // unbind all events in the namespace @@ -95,7 +95,7 @@ an object into `$.fn.bind` with one or more key/value pairs, with the key being the event name and the value being the function to handle the event. ``` -//Binding Multiple Events +// Binding Multiple Events $('p').bind({ 'click': function() { console.log('clicked!'); }, 'mouseover': function() { console.log('hovered!'); } diff --git a/page/events/inside-event-handling-function.md b/page/events/inside-event-handling-function.md index d57a3f8b..bed9a262 100644 --- a/page/events/inside-event-handling-function.md +++ b/page/events/inside-event-handling-function.md @@ -46,12 +46,12 @@ simply do $(this), often following this idiom: ``` ``` -//Preventing a link from being followed - $('a').click(function(e) { - var $this = $(this); - if ($this.attr('href').match('evil')) { - e.preventDefault(); - $this.addClass('evil'); - } - }); +// Preventing a link from being followed +$('a').click(function(e) { + var $this = $(this); + if ($this.attr('href').match('evil')) { + e.preventDefault(); + $this.addClass('evil'); + } +}); ``` diff --git a/page/events/introduction-to-custom-events.md b/page/events/introduction-to-custom-events.md index d06431a9..f7161c9d 100644 --- a/page/events/introduction-to-custom-events.md +++ b/page/events/introduction-to-custom-events.md @@ -27,12 +27,12 @@ on, and it’s controlled by two three-way switches and a clapper: ```
-
-
-
-
-
-
+
+
+
+
+
+
``` @@ -43,31 +43,31 @@ in; they just want to change the state. Without custom events, you might write some code like this: ``` - $('.switch, .clapper').click(function() { - var $light = $(this).parent().find('.lightbulb'); - if ($light.hasClass('on')) { - $light.removeClass('on').addClass('off'); - } else { - $light.removeClass('off').addClass('on'); - } - }); +$('.switch, .clapper').click(function() { + var $light = $(this).parent().find('.lightbulb'); + if ($light.hasClass('on')) { + $light.removeClass('on').addClass('off'); + } else { + $light.removeClass('off').addClass('on'); + } +}); ``` With custom events, your code might look more like this: ``` - $('.lightbulb').bind('changeState', function(e) { - var $light = $(this); - if ($light.hasClass('on')) { - $light.removeClass('on').addClass('off'); - } else { - $light.removeClass('off').addClass('on'); - } - }); - - $('.switch, .clapper').click(function() { - $(this).parent().find('.lightbulb').trigger('changeState'); - }); +$('.lightbulb').bind('changeState', function(e) { + var $light = $(this); + if ($light.hasClass('on')) { + $light.removeClass('on').addClass('off'); + } else { + $light.removeClass('off').addClass('on'); + } +}); + + $('.switch, .clapper').click(function() { + $(this).parent().find('.lightbulb').trigger('changeState'); + }); ``` This last bit of code is not that exciting, but something important has happened: we’ve moved the behavior of the lightbulb to the lightbulb, and away from the switches and the clapper. @@ -76,19 +76,19 @@ Let’s make our example a little more interesting. We’ll add another room to ```
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
``` @@ -99,33 +99,33 @@ this, we’ll add two more custom events to the lightbulbs: `turnOn` and some logic to decide which one the master switch should trigger: ``` - $('.lightbulb') - .bind('changeState', function(e) { - var $light = $(this); - if ($light.hasClass('on')) { - $light.trigger('turnOff'); - } else { - $light.trigger('turnOn'); - } - }) - .bind('turnOn', function(e) { - $(this).removeClass('off').addClass('on'); - }) - .bind('turnOff', function(e) { - $(this).removeClass('on').addClass('off'); - }); - - $('.switch, .clapper').click(function() { - $(this).parent().find('.lightbulb').trigger('changeState'); - }); - - $('#master_switch').click(function() { - if ($('.lightbulb.on').length) { - $('.lightbulb').trigger('turnOff'); - } else { - $('.lightbulb').trigger('turnOn'); - } - }); +$('.lightbulb') + .bind('changeState', function(e) { + var $light = $(this); + if ($light.hasClass('on')) { + $light.trigger('turnOff'); + } else { + $light.trigger('turnOn'); + } + }) + .bind('turnOn', function(e) { + $(this).removeClass('off').addClass('on'); + }) + .bind('turnOff', function(e) { + $(this).removeClass('on').addClass('off'); + }); + +$('.switch, .clapper').click(function() { + $(this).parent().find('.lightbulb').trigger('changeState'); +}); + +$('#master_switch').click(function() { + if ($('.lightbulb.on').length) { + $('.lightbulb').trigger('turnOff'); + } else { + $('.lightbulb').trigger('turnOn'); + } +}); ``` Note how the behavior of the master switch is attached to the master switch; @@ -160,13 +160,13 @@ Here is an example of the usage of `$.fn.bind` and `$.fn.trigger` that uses custom data in both cases: ``` - $(document).bind('myCustomEvent', { foo : 'bar' }, function(e, arg1, arg2) { - console.log(e.data.foo); // 'bar' - console.log(arg1); // 'bim' - console.log(arg2); // 'baz' - }); +$(document).bind('myCustomEvent', { foo : 'bar' }, function(e, arg1, arg2) { + console.log(e.data.foo); // 'bar' + console.log(arg1); // 'bim' + console.log(arg2); // 'baz' +}); - $(document).trigger('myCustomEvent', [ 'bim', 'baz' ]); +$(document).trigger('myCustomEvent', [ 'bim', 'baz' ]); ``` ### A Sample Application @@ -190,15 +190,15 @@ When we’re done, it will look like this:
- - + +
-
-

Search Results for -

-
+
+

Search Results for +

+
``` @@ -240,118 +240,118 @@ class to determine which custom event will be triggered on the corresponding results container. ``` - $.fn.twitterResult = function(settings) { - return this.each(function() { - var $results = $(this), - $actions = $.fn.twitterResult.actions = - $.fn.twitterResult.actions || - $.fn.twitterResult.createActions(), - $a = $actions.clone().prependTo($results), - term = settings.term; - - $results.find('span.search_term').text(term); - - $.each( - ['refresh', 'populate', 'remove', 'collapse', 'expand'], - function(i, ev) { - $results.bind( - ev, - { term : term }, - $.fn.twitterResult.events[ev] - ); - } - ); - - // use the class of each action to figure out - // which event it will trigger on the results panel - $a.find('li').click(function() { - // pass the li that was clicked to the function - // so it can be manipulated if needed - $results.trigger($(this).attr('class'), [ $(this) ]); - }); - }); - }; - - $.fn.twitterResult.createActions = function() { - return $('