-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.onFirst.js
More file actions
42 lines (39 loc) · 1.33 KB
/
jquery.onFirst.js
File metadata and controls
42 lines (39 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
(function($){
"use strict";
var onFirstCounter = +(new Date());
// Returns a function that will only be executed once
var onFirstProxyHandler = function(handler){
var eventKey = 'onfirst_' + (onFirstCounter++);
return function(){
var $element = $(this);
if (!$element.data(eventKey)) {
handler.apply(this, arguments);
$element.data(eventKey, true);
}
};
};
// Alternative to jQuery "one" that will run the handler once for EACH element that triggers the event
$.fn.onFirst = function(events, selector, data, handler){
var eventsMap = {};
// When no selector is passed, this will be direct-bound to the element using "one"
if ($.type(selector) !== 'string') {
return $.fn.one.apply(this, arguments);
}
// Handle either a string of event names or a map of events-and-callbacks for the 1st parameter
if ($.isPlainObject(events)) {
// Intercept each handler with our own one-time callback
$.each(events, function(eventName, callback){
eventsMap[eventName] = onFirstProxyHandler(callback);
});
} else {
// shift params when data was not passed
if (!$.isPlainObject(data)) {
handler = data;
data = null;
}
// Assign the single handler to the events map
eventsMap[events] = onFirstProxyHandler(handler);
}
return this.on(eventsMap, selector, data);
};
})(jQuery);