├── .gitmodules ├── LICENSE ├── Makefile ├── README.md ├── bind-first.jquery.json ├── bower.json ├── build └── header.js ├── dev ├── example │ └── index.html └── jquery.bind-first.js ├── examples ├── index.html └── styles.css ├── release ├── jquery.bind-first-0.2.3.js └── jquery.bind-first-0.2.3.min.js └── tests └── index.html /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "tests/qunit"] 2 | path = tests/qunit 3 | url = https://github.com/jquery/qunit.git 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | ---- 3 | 4 | Copyright (c) 2013 Vladimir Zhuravlev 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | BUILD_DIR = build 2 | RELEASE_DIR = release 3 | 4 | RESOURCES = ${BUILD_DIR}/header.js\ 5 | dev/jquery.bind-first.js\ 6 | 7 | LIB_VER = $(shell node -e "console.log(JSON.parse(require('fs').readFileSync('bind-first.jquery.json').toString()).version);") 8 | DATE = $(shell date) 9 | 10 | COMBINED = ${RELEASE_DIR}/jquery.bind-first-${LIB_VER}.js 11 | MINIFIED = ${RELEASE_DIR}/jquery.bind-first-${LIB_VER}.min.js 12 | 13 | combine: $(RESOURCES) 14 | rm -rf ${RELEASE_DIR} 15 | mkdir ${RELEASE_DIR} 16 | cat $(RESOURCES) | \ 17 | sed 's/@DATE/'"${DATE}"'/' | \ 18 | sed 's/@VERSION/${LIB_VER}/' > $(COMBINED) 19 | # sed -e's/jquery\.bind-first-.*\.min+/jquery.bind-first-${LIB_VER}\.min/' examples/index.html 20 | 21 | min: combine 22 | uglifyjs2 $(COMBINED) --compress --mangle --comments --output $(MINIFIED) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jQuery.bind-first 2 | 3 | bind-first is a tiny jQuery plugin for moving event handler to the beginning of the event queue so that this handler will be executed first, regardless of how many other handlers you had already attached. 4 | 5 | Using jQuery.bind-first you may be sure that your event handler will be executed first, regardless of its actual appearance in the code. 6 | 7 | ## Usage 8 | 9 | ```js 10 | $(/* selector */).onFirst(/* arguments */); // (only jQuery >= 1.7) 11 | $(/* selector */).bindFirst(/* arguments */); 12 | $(/* selector */).delegateFirst(/* arguments */); 13 | $(/* selector */).liveFirst(/* arguments */); 14 | $(/* selector */).oneFirst(/* arguments */); 15 | ``` 16 | 17 | The arguments are completely identical to the corresponding jQuery functions: [bind](http://api.jquery.com/bind/), [delegate](http://api.jquery.com/delegate/), [live](http://api.jquery.com/live/), [one](http://api.jquery.com/one/) and [on](http://api.jquery.com/on/). 18 | 19 | ## Example 20 | 21 | ```js 22 | $('#bind a') 23 | // regular bind: binded first, will be runned second 24 | .bind('click', function() { 25 | alert('regular bind'); 26 | return false; 27 | }) 28 | // bindFirst: binded second, will be runned first 29 | .bindFirst('click', function() { 30 | alert('bindFirst'); 31 | return false; 32 | }); 33 | ``` 34 | 35 | ## License 36 | 37 | ### MIT License 38 | 39 | Copyright (c) 2014 Vladimir Zhuravlev 40 | 41 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 42 | 43 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 44 | 45 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 46 | -------------------------------------------------------------------------------- /bind-first.jquery.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bind-first", 3 | "title": "jQuery.bind-first", 4 | "description": "Tiny jQuery plugin for moving event handler to the beginning of the event queue so that this handler will be executed first, regardless of how many other handlers you had already attached.", 5 | "keywords": [ 6 | "events" 7 | ], 8 | "version": "0.2.3", 9 | "author": { 10 | "name": "Vladimir Zhuravlev", 11 | "url": "https://github.com/private-face/" 12 | }, 13 | "licenses": [ 14 | { 15 | "type": "MIT", 16 | "url": "https://github.com/private-face/jquery.bind-first/blob/master/LICENSE" 17 | } 18 | ], 19 | "homepage": "http://private-face.github.io/jquery.bind-first/", 20 | "dependencies": { 21 | "jquery": ">=1.4.2" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bind-first", 3 | "version": "0.2.3", 4 | "homepage": "https://github.com/private-face/jquery.bind-first", 5 | "authors": [ 6 | "Vladimir Zhuravlev " 7 | ], 8 | "description": "Bind-first allows you to attach a handler to an event be sure that it will be executed first, regardless of how many handlers you already attached.", 9 | "main": "release/jquery.bind-first-0.2.3.min.js", 10 | "keywords": [ 11 | "event" 12 | ], 13 | "license": "MIT", 14 | "dependencies": { 15 | "jquery": ">=1.4.2" 16 | }, 17 | "ignore": [ 18 | "**/.*", 19 | "node_modules", 20 | "bower_components", 21 | "tests", 22 | "build" 23 | ] 24 | } 25 | -------------------------------------------------------------------------------- /build/header.js: -------------------------------------------------------------------------------- 1 | /* 2 | * jQuery.bind-first library v@VERSION 3 | * Copyright (c) 2013 Vladimir Zhuravlev 4 | * 5 | * Released under MIT License 6 | * @license 7 | * 8 | * Date: @DATE 9 | **/ 10 | 11 | -------------------------------------------------------------------------------- /dev/example/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 38 | 39 | 40 |
41 |

bindFirst, delegateFirst & liveFirst examples

42 |

bindFirst

43 |
44 | Click this link 45 |

Source code:

46 | 47 | 48 | 61 |
62 |

delegateFirst

63 |
64 | Click this link 65 |

Source code:

66 | 67 | 68 | 81 |
82 |

liveFirst

83 |
84 | Click this link 85 |

Source code:

86 | 87 | 88 | 101 |
102 |
103 | 112 | 113 | -------------------------------------------------------------------------------- /dev/jquery.bind-first.js: -------------------------------------------------------------------------------- 1 | (function($) { 2 | var splitVersion = $.fn.jquery.split("."); 3 | var major = parseInt(splitVersion[0]); 4 | var minor = parseInt(splitVersion[1]); 5 | 6 | var JQ_LT_17 = (major < 1) || (major == 1 && minor < 7); 7 | 8 | function eventsData($el) { 9 | return JQ_LT_17 ? $el.data('events') : $._data($el[0]).events; 10 | } 11 | 12 | function moveHandlerToTop($el, eventName, isDelegated) { 13 | var data = eventsData($el); 14 | var events = data[eventName]; 15 | 16 | if (!JQ_LT_17) { 17 | var handler = isDelegated ? events.splice(events.delegateCount - 1, 1)[0] : events.pop(); 18 | events.splice(isDelegated ? 0 : (events.delegateCount || 0), 0, handler); 19 | 20 | return; 21 | } 22 | 23 | if (isDelegated) { 24 | data.live.unshift(data.live.pop()); 25 | } else { 26 | events.unshift(events.pop()); 27 | } 28 | } 29 | 30 | function moveEventHandlers($elems, eventsString, isDelegate) { 31 | var events = eventsString.split(/\s+/); 32 | $elems.each(function() { 33 | for (var i = 0; i < events.length; ++i) { 34 | var pureEventName = $.trim(events[i]).match(/[^\.]+/i)[0]; 35 | moveHandlerToTop($(this), pureEventName, isDelegate); 36 | } 37 | }); 38 | } 39 | 40 | function makeMethod(methodName) { 41 | $.fn[methodName + 'First'] = function() { 42 | var args = $.makeArray(arguments); 43 | var eventsString = args.shift(); 44 | 45 | if (eventsString) { 46 | $.fn[methodName].apply(this, arguments); 47 | moveEventHandlers(this, eventsString); 48 | } 49 | 50 | return this; 51 | } 52 | } 53 | 54 | // bind 55 | makeMethod('bind'); 56 | 57 | // one 58 | makeMethod('one'); 59 | 60 | // delegate 61 | $.fn.delegateFirst = function() { 62 | var args = $.makeArray(arguments); 63 | var eventsString = args[1]; 64 | 65 | if (eventsString) { 66 | args.splice(0, 2); 67 | $.fn.delegate.apply(this, arguments); 68 | moveEventHandlers(this, eventsString, true); 69 | } 70 | 71 | return this; 72 | }; 73 | 74 | // live 75 | $.fn.liveFirst = function() { 76 | var args = $.makeArray(arguments); 77 | 78 | // live = delegate to the document 79 | args.unshift(this.selector); 80 | $.fn.delegateFirst.apply($(document), args); 81 | 82 | return this; 83 | }; 84 | 85 | // on (jquery >= 1.7) 86 | if (!JQ_LT_17) { 87 | $.fn.onFirst = function(types, selector) { 88 | var $el = $(this); 89 | var isDelegated = typeof selector === 'string'; 90 | 91 | $.fn.on.apply($el, arguments); 92 | 93 | // events map 94 | if (typeof types === 'object') { 95 | for (type in types) 96 | if (types.hasOwnProperty(type)) { 97 | moveEventHandlers($el, type, isDelegated); 98 | } 99 | } else if (typeof types === 'string') { 100 | moveEventHandlers($el, types, isDelegated); 101 | } 102 | 103 | return $el; 104 | }; 105 | } 106 | 107 | })(jQuery); 108 | -------------------------------------------------------------------------------- /examples/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 |

bindFirst, delegateFirst & liveFirst examples

13 |

bindFirst

14 |
15 | Click this link 16 |

Source code:

17 | 18 | 19 | 32 |
33 |

delegateFirst

34 |
35 | Click this link 36 |

Source code:

37 | 38 | 39 | 52 |
53 |

liveFirst

54 |
55 | Click this link 56 |

Source code:

57 | 58 | 59 | 72 |
73 |
74 | 83 | 84 | 85 | -------------------------------------------------------------------------------- /examples/styles.css: -------------------------------------------------------------------------------- 1 | html, body { 2 | margin:0; 3 | padding: 0; 4 | background: #fff; 5 | color: #333; 6 | } 7 | #main { 8 | width: 800px; 9 | background: #f5f5f5; 10 | margin: 0 auto; 11 | padding: 1px 20px; 12 | } 13 | h2 { 14 | border-bottom: 2px solid #ddd; 15 | } 16 | h3 { 17 | font: 12px 'Monaco', 'Courier New', monospace; 18 | margin: 20px 0 0; 19 | } 20 | .example code { 21 | display: block; 22 | font: 10px 'Monaco', 'Courier New', monospace; 23 | white-space: pre; 24 | background: #FBFFCE; 25 | border: 1px solid #EFF2C7; 26 | border-radius: 4px; 27 | padding: 5px 10px; 28 | } 29 | -------------------------------------------------------------------------------- /release/jquery.bind-first-0.2.3.js: -------------------------------------------------------------------------------- 1 | /* 2 | * jQuery.bind-first library v0.2.3 3 | * Copyright (c) 2013 Vladimir Zhuravlev 4 | * 5 | * Released under MIT License 6 | * @license 7 | * 8 | * Date: Thu Feb 6 10:13:59 ICT 2014 9 | **/ 10 | 11 | (function($) { 12 | var splitVersion = $.fn.jquery.split("."); 13 | var major = parseInt(splitVersion[0]); 14 | var minor = parseInt(splitVersion[1]); 15 | 16 | var JQ_LT_17 = (major < 1) || (major == 1 && minor < 7); 17 | 18 | function eventsData($el) { 19 | return JQ_LT_17 ? $el.data('events') : $._data($el[0]).events; 20 | } 21 | 22 | function moveHandlerToTop($el, eventName, isDelegated) { 23 | var data = eventsData($el); 24 | var events = data[eventName]; 25 | 26 | if (!JQ_LT_17) { 27 | var handler = isDelegated ? events.splice(events.delegateCount - 1, 1)[0] : events.pop(); 28 | events.splice(isDelegated ? 0 : (events.delegateCount || 0), 0, handler); 29 | 30 | return; 31 | } 32 | 33 | if (isDelegated) { 34 | data.live.unshift(data.live.pop()); 35 | } else { 36 | events.unshift(events.pop()); 37 | } 38 | } 39 | 40 | function moveEventHandlers($elems, eventsString, isDelegate) { 41 | var events = eventsString.split(/\s+/); 42 | $elems.each(function() { 43 | for (var i = 0; i < events.length; ++i) { 44 | var pureEventName = $.trim(events[i]).match(/[^\.]+/i)[0]; 45 | moveHandlerToTop($(this), pureEventName, isDelegate); 46 | } 47 | }); 48 | } 49 | 50 | function makeMethod(methodName) { 51 | $.fn[methodName + 'First'] = function() { 52 | var args = $.makeArray(arguments); 53 | var eventsString = args.shift(); 54 | 55 | if (eventsString) { 56 | $.fn[methodName].apply(this, arguments); 57 | moveEventHandlers(this, eventsString); 58 | } 59 | 60 | return this; 61 | } 62 | } 63 | 64 | // bind 65 | makeMethod('bind'); 66 | 67 | // one 68 | makeMethod('one'); 69 | 70 | // delegate 71 | $.fn.delegateFirst = function() { 72 | var args = $.makeArray(arguments); 73 | var eventsString = args[1]; 74 | 75 | if (eventsString) { 76 | args.splice(0, 2); 77 | $.fn.delegate.apply(this, arguments); 78 | moveEventHandlers(this, eventsString, true); 79 | } 80 | 81 | return this; 82 | }; 83 | 84 | // live 85 | $.fn.liveFirst = function() { 86 | var args = $.makeArray(arguments); 87 | 88 | // live = delegate to the document 89 | args.unshift(this.selector); 90 | $.fn.delegateFirst.apply($(document), args); 91 | 92 | return this; 93 | }; 94 | 95 | // on (jquery >= 1.7) 96 | if (!JQ_LT_17) { 97 | $.fn.onFirst = function(types, selector) { 98 | var $el = $(this); 99 | var isDelegated = typeof selector === 'string'; 100 | 101 | $.fn.on.apply($el, arguments); 102 | 103 | // events map 104 | if (typeof types === 'object') { 105 | for (type in types) 106 | if (types.hasOwnProperty(type)) { 107 | moveEventHandlers($el, type, isDelegated); 108 | } 109 | } else if (typeof types === 'string') { 110 | moveEventHandlers($el, types, isDelegated); 111 | } 112 | 113 | return $el; 114 | }; 115 | } 116 | 117 | })(jQuery); 118 | -------------------------------------------------------------------------------- /release/jquery.bind-first-0.2.3.min.js: -------------------------------------------------------------------------------- 1 | /* 2 | * jQuery.bind-first library v0.2.3 3 | * Copyright (c) 2013 Vladimir Zhuravlev 4 | * 5 | * Released under MIT License 6 | * @license 7 | * 8 | * Date: Thu Feb 6 10:13:59 ICT 2014 9 | **/ 10 | (function(t){function e(e){return u?e.data("events"):t._data(e[0]).events}function n(t,n,r){var i=e(t),a=i[n];if(!u){var s=r?a.splice(a.delegateCount-1,1)[0]:a.pop();return a.splice(r?0:a.delegateCount||0,0,s),void 0}r?i.live.unshift(i.live.pop()):a.unshift(a.pop())}function r(e,r,i){var a=r.split(/\s+/);e.each(function(){for(var e=0;a.length>e;++e){var r=t.trim(a[e]).match(/[^\.]+/i)[0];n(t(this),r,i)}})}function i(e){t.fn[e+"First"]=function(){var n=t.makeArray(arguments),i=n.shift();return i&&(t.fn[e].apply(this,arguments),r(this,i)),this}}var a=t.fn.jquery.split("."),s=parseInt(a[0]),f=parseInt(a[1]),u=1>s||1==s&&7>f;i("bind"),i("one"),t.fn.delegateFirst=function(){var e=t.makeArray(arguments),n=e[1];return n&&(e.splice(0,2),t.fn.delegate.apply(this,arguments),r(this,n,!0)),this},t.fn.liveFirst=function(){var e=t.makeArray(arguments);return e.unshift(this.selector),t.fn.delegateFirst.apply(t(document),e),this},u||(t.fn.onFirst=function(e,n){var i=t(this),a="string"==typeof n;if(t.fn.on.apply(i,arguments),"object"==typeof e)for(type in e)e.hasOwnProperty(type)&&r(i,type,a);else"string"==typeof e&&r(i,e,a);return i})})(jQuery); -------------------------------------------------------------------------------- /tests/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | jquery.bind-first tests 5 | 6 | 7 | 8 | 9 | 10 | 14 | 15 | 16 | 17 | 20 | 21 | 22 | 23 | 26 | 27 | 28 | 29 | 32 | 33 | 34 | 35 | 38 | 39 | 40 | 41 | 44 | 45 | 46 | 47 | 50 | 51 | 52 | 53 | 56 | 57 | 58 | 59 | 62 | 63 | 64 | 65 | 68 | 69 | 70 | 71 |

jquery.bind-first tests

72 |

73 |
74 |

75 |
    76 |
    77 | 78 |
    79 |
    80 | 81 |
    82 | 164 | 165 | --------------------------------------------------------------------------------