jQuery custom event å¿ç¨ç·¨
åç½®ã
custom eventã¨ã¯ä½ã?(åç½®ãã®åç½®ã)
- ãã©ã¦ã¶ããµãã¼ããã¦ããeventã§ã¯ãªãç¬èªå®ç¾©eventã
- clickã¨ãã¯ãã©ã¦ã¶ããµãã¼ããã¦ããevent
- ã¦ã¼ã¶ã®ã¢ã¯ã·ã§ã³ããã©ã¦ã¶ã®ç¶æ
çã«ãã£ã¦ç´æ¥çºç«ããããã¨ã¯ãªã
- click eventã¯ãã¦ã¼ã¶ããã¦ã¹ãã¤ã³ã¿ãè¦ç´ ã®ä¸ã«ç§»åå¾ã«ãã¦ã¹ã®ãã¿ã³ãæ¼ãã¨çºç«ããã
- DOMContentLoaded eventã¯ããã¼ã¸ã®HTMLããã¦ã³ãã¼ãå®äºãããã¹ã¦ãã¼ã¹ãããã¨çºç«ããã
- jQueryã§ã¯trigger,triggerHandler methodã§eventãã¦ã¼ã¶ã®ã¢ã¯ã·ã§ã³çã«é¢ä¿ãªãçºç«ãããã¨ãåºæ¥ã
- custom eventãçºç«åºæ¥ã
- éã«è¨ãã¨custom eventã¯JavaScriptã®å´ããããçºç«ãããªã
- custom eventãçºç«åºæ¥ã
以åThe JUI 2009 Returnsã§è©±ããã¾ããã
http://dl.dropbox.com/u/612874/slide/jui20090710/s6maker.html
åç½®ãæ¬ç·¨
eventã®çºç«ã«ã¤ãã¦ã¯ãäºåã«ç»é²ããã¦ããé¢æ°(eventHandler)ãå®è¡ããããããã«æã£ã¦ãã人ãå¤ãã¨æããã
custom eventã§é常ã®ããã°ã©ã 並ããã以ä¸ã®è±ããªè¡¨ç¾ãã§ããã¨ãã話ããã¾ãã
æ®éã®ããã°ã©ã ã§åºæ¥ããã¨ãcustom eventã§ããã£ã¦ã¿ã
(é常ã®)é¢æ°å¼ã³åºã
http://jsdo.it/monjudoh/bps41-custom-event-function-call
uu.ready(function(){ function fun(){ uu.log('fun is called.'); } fun(); $('#target').bind('fun',function(ev){ uu.log('fun is triggered.'); }); $('#target').trigger('fun'); });
ä¸çªåºæ¬çãªãã®ã§ãã
function é¢æ°å(){}ã§é¢æ°ãå®ç¾©ããé¢æ°å()ã§é¢æ°ãå¼ã³åºãã¨ããã®ã¨åããããªäºã¯ã
.bind(eventType,é¢æ°)ã§ç¹å®ã®è¦ç´ ã«eventãè²¼ãã¤ãã.trigger(eventType)ã§çºç«ãããã¨ãããã¨ã§åºæ¥ã¾ãã
å¼æ°ä»ãé¢æ°å¼ã³åºã
http://jsdo.it/monjudoh/bps41-custom-event-function-call-with-args
uu.ready(function(){ function fun(a,b){ uu.log('fun is called. @,@.',a,b); } fun('hello','world'); $('#target').bind('fun',function(ev,a,b){ uu.log('fun is triggered. @,@.',a,b); }); $('#target').trigger('fun',['hello','world']); });
.bind(eventType,é¢æ°)ã§æ¸¡ãé¢æ°(callbacké¢æ°)ã®ç¬¬ä¸å¼æ°ã¯event objectã§ãã
.trigger(eventType)ã§çºç«ãããã¨callbacké¢æ°ã®ç¬¬ä¸å¼æ°ã¨ãã¦event objectã渡ãããããã§ãã
ã§ã¯ç¬¬äºå¼æ°ä»¥éã¯ã©ããªã£ã¦ããã®ãã¨ããã¨ã
第äºå¼æ°ã«é
å以å¤ã渡ãã¨ãcallbacké¢æ°ã®ç¬¬äºå¼æ°ã¨ãã¦ããã渡ããã
第äºå¼æ°ã«é
åã渡ãã¨ãcallbacké¢æ°ã®ç¬¬äºå¼æ°ä»¥éã«å±éããã¦æ¸¡ããã¾ãã
ã¤ã³ã¹ã¿ã³ã¹å¤æ°ã¸ã®ã¢ã¯ã»ã¹ãå«ãã¡ã½ããã®å¼ã³åºã
http://jsdo.it/monjudoh/bps41-custom-event-method-call
uu.ready(function(){ var obj = { prop : 'hogehoge' ,method : method }; function method(){ uu.log('method is called.@.',this.prop); } obj.method(); $('#target').data('prop','hogehoge'); $('#target').bind('method',function(ev){ uu.log('method is triggered.@.', $(this).data('prop') ); }); $('#target').trigger('method'); });
ããã¾ã§ã¯é¢æ°å¼ã³åºãã£ã½ãäºããã話ã§ãããã
ä»åº¦ã¯ãªãã¸ã§ã¯ãã®ã¡ã½ããå¼ã³åºãã£ã½ãäºããã話ã§ãã
custom eventã§ã¤ã³ã¹ã¿ã³ã¹å¤æ°ã£ã½ããã®ãã©ãæ±ããã¨ããã¨ã
jQueryã®data methodã使ãã¾ãã
data methodã¯DOMè¦ç´ ãã¨ã«ç¬ç«ããé£æ³é
åãæ±ããã®ã§ãã
Class
http://jsdo.it/monjudoh/bps41-custom-event-Class
function Class(){} Class.prototype.method = method; function method(){ uu.log('method is called.@.',this.prop); } $('.clazz').live('method',function(ev){ uu.log('method is triggered.@.', $(this).data('prop') ); }); uu.ready(function(){ var obj1 = new Class(); obj1.prop = 'hogehoge'; obj1.method(); var obj2 = new Class(); obj2.prop = 'fugafuga'; obj2.method(); $('<div id="target1" class="clazz"></div><div id="target2" class="clazz"></div>').appendTo('body'); $('#target1').data('prop','hogehoge'); $('#target1').trigger('method'); $('#target2').data('prop','fugafuga'); $('#target2').trigger('method'); });
Classãã©ãåç¾ãããã¨ãã話ã
ããã«ã¯bindã«ããè¦ç´ ã¸ã®eventè²¼ä»ãã§ã¯ãªãã
live methodã«ããCSSã»ã¬ã¯ã¿ã¸ã®eventã®è²¼ä»ãã使ãã¾ãã
ããCSSã»ã¬ã¯ã¿(ä¾ãã°classã»ã¬ã¯ã¿)ã«å¯¾ãã¦liveã§eventãè²¼ãä»ãã
ãã®å¾ãã®CSSã»ã¬ã¯ã¿ãæºããè¦ç´ (ä¾ãã°classå±æ§ã«è©²å½classãå«ã¾ãã¦ãã)ãä½æããã®ã¯ã
Classãå®ç¾©ãããã®ã¤ã³ã¹ã¿ã³ã¹ãçæããã®ã«ä¼¼ã¦ãã¾ãã
liveã«ã¤ãã¦ã¯ä»¥åBPStudyã§çºè¡¨ãã¾ãããjQuery1.4aでのlive event/special event - 文殊堂
mixin
http://jsdo.it/monjudoh/bps41-custom-event-mixin
var module = {method:method}; function method(){ uu.log('method is called.@.',this.prop); } $('.clazz').live('method',function(ev){ uu.log('method is triggered.@.', $(this).data('prop') ); }); uu.ready(function(){ var obj = {prop:'hogehoge'}; try { uu.log("method追å åã«methodå¼ã³åºã"); obj.method(); } catch(e) { uu.log(e); } $.extend(obj,module); uu.log("method追å å¾ã«methodå¼ã³åºã"); obj.method(); $('<div id="target">').appendTo('body'); $('#target').data('prop','hogehoge'); uu.log("addClassåã«trigger"); $('#target').trigger('method'); $('#target').addClass('clazz'); uu.log("addClasså¾ã«trigger"); $('#target').trigger('method'); });
å¾ä»ã§objectã«method群ãçããmixinã§ãã
äºåã«ãliveã§classã»ã¬ã¯ã¿ã«eventãè²¼ãä»ãã¦ããç¶æ
ã§ã
対象ã®è¦ç´ ã®classå±æ§ã«è©²å½classãå¾ä»ã§è¿½å ãã¦ãã¾ãã°å®ç¾ã§ãã¾ãã
liveã§eventãè²¼ãã¤ããCSSã»ã¬ã¯ã¿ãæºããããã«ãªãããã§ãããã
callback
http://jsdo.it/monjudoh/bps41-custom-event-callback
function WebApiWrapper(){} WebApiWrapper.prototype.call = call; function call(){ // Timerã§å¤§ä½500mså¾ã«éåæã§callbackãå®è¡ãã // WebAPIçãXHRã§å©ãã¦callbackãå®è¡ããã®ã®ä»£ãã var self = this; setTimeout(function(){ (self.callback || function(){})('result'); },500); } $('.clazz').live('call',function(ev){ var self = this; // Timerã§å¤§ä½500mså¾ã«éåæã§callbackãå®è¡ãã // WebAPIçãXHRã§å©ãã¦callbackãå®è¡ããã®ã®ä»£ãã setTimeout(function(){ $(self).trigger('callback',['result']); },500); }); $('.clazz').live('callback',function(ev,result){ uu.log('callback is triggered.@.', result); }); uu.ready(function(){ var obj1 = new WebApiWrapper(); obj1.callback = function(result){ uu.log('callback is called.@.',result); }; obj1.call(); $('<div id="target1" class="clazz">').appendTo('body'); $('#target1').trigger('call'); });
JavaScriptã§ã¯Ajaxçcallbacké¢æ°ãç»é²ãã
ä½ãã®ã¿ã¤ãã³ã°ã§ãããå¼ã°ããã¨ãã£ãã³ã¼ããé »ç¹ã«æ¸ãã¾ãã
ãããcustom eventã§åç¾ããã«ã¯ã©ãããã°ãããã¨ããã¨ã
ãããããeventãããã¦callbackç»é²âä½ãã®ã¿ã¤ãã³ã°ã§ãããå®è¡ã
ã¨ãããã®ãªã®ã§ç¹ã«é å¼µããã¨ã¯ããã¾ããã
æ®éã®ããã°ã©ã ã§ã¯åºæ¥ãªããã¨ããã
mixout
http://jsdo.it/monjudoh/bps41-custom-event-mixout
$('.clazz').live('method1',function(ev){ uu.log('method1 is triggered.@.', $(this).data('prop') ); }); $('.clazz').live('method2',function(ev){ uu.log('method2 is triggered.@.', $(this).data('prop') ); }); uu.ready(function(){ $('<div id="target">').appendTo('body'); $('#target').data('prop','hogehoge'); uu.log("addClassåã«trigger"); $('#target').trigger('method1'); $('#target').trigger('method2'); $('#target').addClass('clazz'); uu.log("addClasså¾ã«trigger"); $('#target').trigger('method1'); $('#target').trigger('method2'); $('#target').removeClass('clazz'); uu.log("removeClasså¾ã«trigger"); $('#target').trigger('method1'); $('#target').trigger('method2'); });
mixinã®é
ã§ã¯è¦ç´ ã«classãæ¯ããã¨ã§ãå¾ä»ã§è¦ç´ ã¨live eventãç´ä»ãã¾ããã
ãããéã«è¦ç´ ããclassãå¤ãã¦ãã¾ãã°ãç´ä»ãã解é¤ãããã¨ãåºæ¥ã¾ãã
æ®éã®ããã°ã©ãã³ã°ã§ã®mixinã¯method群ãæã¤moduleãåãè¾¼ãã§ã
ãã®method群ãçãã訳ã§ããããã®éã
moduleãæã¤method群ãåã³å¤ãã¦ãã¾ãã¨ãããã¨ãã
custom eventã§ã¯å®ç¾å¯è½ãªããã§ãã
ååã®n(â§0)åã®é¢æ°ã®å¼ã³åºã
http://jsdo.it/monjudoh/bps41-custom-event-many-function-call
uu.ready(function(){ uu.log("bindåã«trigger"); $('#target').trigger('fun'); $('#target').bind('fun',function(ev){ uu.log('fun(1) is triggered.'); }); uu.log("1åbindå¾ã«trigger"); $('#target').trigger('fun'); $('#target').bind('fun',function(ev){ uu.log('fun(2) is triggered.'); }); $('#target').bind('fun',function(ev){ uu.log('fun(3) is triggered.'); }); uu.log("追å ã§2åbindå¾ã«trigger"); $('#target').trigger('fun'); });
æ®éã®ããã°ã©ãã³ã°ã§ã¯ããååã®é¢æ°ãå¼ãã å ´åã«å®è¡ãããé¢æ°ã¯ä¸ã¤ã§ãã
åãååã§è¤æ°ã®é¢æ°ãå®ç¾©ãããã¨ã¯åºæ¥ã¾ãã(ãªã¼ãã¼ãã¼ãã§ãã¦ãã·ã°ããã£ãååã«å«ããã°1ã¤ã§ã)ã
ã¾ããæªå®ç¾©ã®é¢æ°ãå¼ã¶ãã¨ã¯åºæ¥ã¾ãã(method missingçã§ã¨ã©ã¼ãåé¿ã§ããè¨èªã¯ããã¾ã)ã
custom eventã§ã¯ãããåºæ¥ã¾ãã
event handlerã®ç»é²ã¨eventã®çºç«ãªã®ã§å½ããåã§ãã
eventãã®ãã®ã¨ãã¦è¦ãåã«ã¯å½ããåã§ããã
ãããé¢æ°ã®å®ç¾©ã¨å¼ã³åºãã¨ãã¦æããã¨ã
- å®ç¾©ããã¦ãããã©ãããæ°ã«ããå®å ¨ã«å¼ã³åºãã
- 好ããªã ã追å ã§å®ç¾©ã§ãã
- æ®éã®é¢æ°ã ã£ããå é¢æ°ãå¤æ°ã«ããã¯ã¢ãããã追å ã®å¦çã¨å é¢æ°ã®å¼ã³åºãã¨ããå 容ã®é¢æ°ã§å®ç¾©ãä¸æ¸ããããã¨ã«ãªã
ã¨ããã¹ã¼ãã¼é¢æ°ã¨è¦ããã¨ãåºæ¥ãã®ã§ãã
ã¾ã¨ã
custom eventã§ãClassãmixin moduleçãæ®éã®ããã°ã©ãã³ã°ã§ä½¿ãæ§é ãã·ãã¥ã¬ã¼ãã§ãã¾ãã
eventã¨ãã¦è¦ãã°å½ããåã®ãã¨ã§ããå¥ã®æãæ¹ãããã¨åããã¨ã ã£ãããã¾ãã
ããããçããã°ã³ã¼ãã®ä¸é¨ãcustom eventãã¼ã¹ã«æ¸ãæãããã¨ãã
ã³ã¼ãã®æ§é ã«ç¸ããããã¨ãªãè¡ããã¨ãåºæ¥ãã®ã§ãã