Top Banner
James Greene http://jamesgreene.net/
19

Leveraging jQuery's Special Events API (JSConf 2012)

Jul 15, 2015

Download

Technology

James Greene
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Leveraging jQuery's Special Events API (JSConf 2012)

James Greene

http://jamesgreene.net/

Page 2: Leveraging jQuery's Special Events API (JSConf 2012)

“Bang, Bang”

(Just kidding!) 2

Page 3: Leveraging jQuery's Special Events API (JSConf 2012)

The Problem

I desire…

3

Page 4: Leveraging jQuery's Special Events API (JSConf 2012)

$("body").on("textSelect", function($event) {

console.log("Text was selected!");

});

4

WANTED!

DEAD OR ALIVE

Page 5: Leveraging jQuery's Special Events API (JSConf 2012)

$("body").on("textSelect", function($event) {

console.log("Text was selected!");

});

$("body").on("mouseup", function($event) {

if (window.getSelection().rangeCount > 0) {

$("body").trigger("textSelect");

}

}

5

Cheesy!

Page 6: Leveraging jQuery's Special Events API (JSConf 2012)

$.fn.onTextSelect = function(callback) {

this.on("mouseup", function($event) {

if (window.getSelection().rangeCount > 0) {

callback($event);

}

}

}

$("body").onTextSelect(function($event) {

console.log("Text was selected!");

});

6

Better…?

Page 7: Leveraging jQuery's Special Events API (JSConf 2012)

jQuery Event System

Special Events API

7

Page 8: Leveraging jQuery's Special Events API (JSConf 2012)

jQuery 1.3:

Special Events API

…but no official documentation

jQuery 1.7:

Major overhaul to Special Events API

…but still no official documentation

Official documentation? Now in progress….

Brief History

8

Page 9: Leveraging jQuery's Special Events API (JSConf 2012)

1.Allow hooks for “fixing” events

2.Allow for creation of custom events

ready

mouseenter

mouseleave

*

Responsibilities

9

Page 10: Leveraging jQuery's Special Events API (JSConf 2012)

$.event.special.MyEvent = {

noBubble: false,

bindType: "otherEventType",

delegateType: "otherEventType",

handle: function($event, data) { … },

};

Custom Event: Syntax #1

NOTE: Almost all of these are optional to set (in varying combinations).10

Page 11: Leveraging jQuery's Special Events API (JSConf 2012)

$.event.special.MyEvent = {

noBubble: false,

setup: function(data, namespaces, handler) { … },

teardown: function() { … },

add: function(handleObj) { … },

remove: function(handleObj) { … },

trigger: function($event, data) { … },

_default: function($event, data) { … }

};

Custom Event: Syntax #2

NOTE: Almost all of these are optional to set (in varying combinations).11

Page 12: Leveraging jQuery's Special Events API (JSConf 2012)

Demo Time

Making the magic happen!

12

Page 13: Leveraging jQuery's Special Events API (JSConf 2012)

Usage:

$(…).on("multiclick", { clicks: 3 },

function($event) {

console.log("Thrice!");

}

);

Demo pages:

Basic

Nested elements

Code:

http://github.com/JamesMGreene/jquery.multiclick/

Demo #1: multiclick

13

Page 14: Leveraging jQuery's Special Events API (JSConf 2012)

Usage:

$(…).on("textSelect", function($event) {

console.log("Text was selected!");

});

Demo page:

Basic

Code:

http://github.com/JamesMGreene/jquery.textSelect/

Demo #2: textSelect

14

Page 15: Leveraging jQuery's Special Events API (JSConf 2012)

throw new RangeError(

"A demo does not exist at this index");

Demo #3: swipe

15

Page 16: Leveraging jQuery's Special Events API (JSConf 2012)

Aftermath

Lessons Learned

16

Page 17: Leveraging jQuery's Special Events API (JSConf 2012)

Surprise!

Textual selection event was added to IE9+.

Sort of….

17

Lessons Learned

Page 18: Leveraging jQuery's Special Events API (JSConf 2012)

Special Events API is a dependency of jQuery Mobile:

tap

scrollstart / scrollstop

swipe / swipeleft / swiperight

orientationchange

throttledresize

18

Lessons Learned

Page 19: Leveraging jQuery's Special Events API (JSConf 2012)

19

Questions?

James Greenehttp://jamesgreene.net/