Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

refactor(jqLite): stop patching individual jQuery methods #7288

Merged
merged 1 commit into from
May 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/Angular.js
Original file line number Diff line number Diff line change
Expand Up @@ -1430,8 +1430,10 @@ function snake_case(name, separator){
}

function bindJQuery() {
var originalCleanData;
// bind to jQuery if present;
jQuery = window.jQuery;

// reset to jQuery or default to us.
if (jQuery) {
jqLite = jQuery;
Expand All @@ -1442,14 +1444,22 @@ function bindJQuery() {
injector: JQLitePrototype.injector,
inheritedData: JQLitePrototype.inheritedData
});
// Method signature:
// jqLitePatchJQueryRemove(name, dispatchThis, filterElems, getterIfNoArguments)
jqLitePatchJQueryRemove('remove', true, true, false);
jqLitePatchJQueryRemove('empty', false, false, false);
jqLitePatchJQueryRemove('html', false, false, true);

originalCleanData = jQuery.cleanData;
// Prevent double-proxying.
originalCleanData = originalCleanData.$$original || originalCleanData;

jQuery.cleanData = function(elems) {
for (var i = 0, elem; (elem = elems[i]) != null; i++) {
jQuery(elem).triggerHandler('$destroy');
}
originalCleanData(elems);
};
jQuery.cleanData.$$original = originalCleanData;
} else {
jqLite = JQLite;
}

angular.element = jqLite;
}

Expand Down
43 changes: 0 additions & 43 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,49 +136,6 @@ function camelCase(name) {
replace(MOZ_HACK_REGEXP, 'Moz$1');
}

/////////////////////////////////////////////
// jQuery mutation patch
//
// In conjunction with bindJQuery intercepts all jQuery's DOM destruction apis and fires a
// $destroy event on all DOM nodes being removed.
//
/////////////////////////////////////////////

function jqLitePatchJQueryRemove(name, dispatchThis, filterElems, getterIfNoArguments) {
var originalJqFn = jQuery.fn[name];
originalJqFn = originalJqFn.$original || originalJqFn;
removePatch.$original = originalJqFn;
jQuery.fn[name] = removePatch;

function removePatch(param) {
// jshint -W040
var list = filterElems && param ? [this.filter(param)] : [this],
fireEvent = dispatchThis,
set, setIndex, setLength,
element, childIndex, childLength, children;

if (!getterIfNoArguments || param != null) {
while(list.length) {
set = list.shift();
for(setIndex = 0, setLength = set.length; setIndex < setLength; setIndex++) {
element = jqLite(set[setIndex]);
if (fireEvent) {
element.triggerHandler('$destroy');
} else {
fireEvent = !fireEvent;
}
for(childIndex = 0, childLength = (children = element.children()).length;
childIndex < childLength;
childIndex++) {
list.push(jQuery(children[childIndex]));
}
}
}
}
return originalJqFn.apply(this, arguments);
}
}

var SINGLE_TAG_REGEXP = /^<(\w+)\s*\/?>(?:<\/\1>|)$/;
var HTML_REGEXP = /<|&#?\w+;/;
var TAG_NAME_REGEXP = /<([\w:]+)/;
Expand Down
10 changes: 0 additions & 10 deletions test/jQueryPatchSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ if (window.jQuery) {

describe('$detach event', function() {

it('should fire on detach()', function() {
doc.find('span').detach();
});

it('should fire on remove()', function() {
doc.find('span').remove();
});
Expand Down Expand Up @@ -83,12 +79,6 @@ if (window.jQuery) {

describe('$detach event is not invoked in too many cases', function() {

it('should fire only on matched elements on detach(selector)', function() {
doc.find('span').detach('.second');
expect(spy2).toHaveBeenCalled();
expect(spy2.callCount).toEqual(1);
});

it('should fire only on matched elements on remove(selector)', function() {
doc.find('span').remove('.second');
expect(spy2).toHaveBeenCalled();
Expand Down