-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.destroy-event.js
58 lines (46 loc) · 1.65 KB
/
jquery.destroy-event.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('jquery'));
} else {
factory(root.jQuery);
}
} (this, function ($) {
'use strict';
// Detect if support is already added by third-party code
var oldClean,
hasSupport;
// Delete any third-party code using special "destroy" event
// before testing, because it doesn't work correctly
delete $.event.special.destroy;
$('<div></div>').on('destroy', function () {
hasSupport = true;
}).remove();
if (hasSupport) {
return;
}
// Finally hook into jQuery to add support for the "destroy" event
oldClean = $.cleanData;
$.cleanData = function (elems) {
var i,
elem,
data;
for (i = 0; (elem = elems[i]) != null; i += 1) {
// Only trigger remove when necessary to save time
// The flag _destroy_event_triggered is just to avoid trigger the destroy again
// in cases like:
// $(elem).on('destroy', function () { $(elem).remove(); });
// $(elem).remove();
// When the remove is called in the destroy handler it will not fire the destroy again.
data = $._data(elem);
if (data && data.events && data.events.destroy && !data._destroy_event_triggered) {
data._destroy_event_triggered = true;
$(elem).triggerHandler('destroy');
}
}
oldClean(elems);
};
return $;
}));