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

Commit

Permalink
perf(jqLite): expose the low-level jqLite.data/removeData calls
Browse files Browse the repository at this point in the history
- updated the internal jqLite helpers to use the low-level jqLite.data/removeData to avoid unnecessary jq wrappers and loops
- updated $compile to use the low-level jqLite.data/removeData to avoid unnecessary jq wrappers at link time
  • Loading branch information
jbedard authored and rodyhaddad committed Jul 18, 2014
1 parent a160f76 commit e4ba894
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
24 changes: 14 additions & 10 deletions src/jqLite.js
Expand Up @@ -411,25 +411,22 @@ function jqLiteController(element, name) {
}

function jqLiteInheritedData(element, name, value) {
element = jqLite(element);

// if element is the document object work with the html element instead
// this makes $(document).scope() possible
if(element[0].nodeType == 9) {
element = element.find('html');
if(element.nodeType == 9) {
element = element.documentElement;
}
var names = isArray(name) ? name : [name];

while (element.length) {
var node = element[0];
while (element) {
for (var i = 0, ii = names.length; i < ii; i++) {
if ((value = element.data(names[i])) !== undefined) return value;
if ((value = jqLite.data(element, names[i])) !== undefined) return value;
}

// If dealing with a document fragment node with a host element, and no parent, use the host
// element as the parent. This enables directives within a Shadow DOM or polyfilled Shadow DOM
// to lookup parent controllers.
element = jqLite(node.parentNode || (node.nodeType === 11 && node.host));
element = element.parentNode || (element.nodeType === 11 && element.host);
}
}

Expand Down Expand Up @@ -512,18 +509,25 @@ function getAliasedAttrName(element, name) {
return (nodeName === 'INPUT' || nodeName === 'TEXTAREA') && ALIASED_ATTR[name];
}

forEach({
data: jqLiteData,
removeData: jqLiteRemoveData
}, function(fn, name) {
JQLite[name] = fn;
});

forEach({
data: jqLiteData,
inheritedData: jqLiteInheritedData,

scope: function(element) {
// Can't use jqLiteData here directly so we stay compatible with jQuery!
return jqLite(element).data('$scope') || jqLiteInheritedData(element.parentNode || element, ['$isolateScope', '$scope']);
return jqLite.data(element, '$scope') || jqLiteInheritedData(element.parentNode || element, ['$isolateScope', '$scope']);
},

isolateScope: function(element) {
// Can't use jqLiteData here directly so we stay compatible with jQuery!
return jqLite(element).data('$isolateScope') || jqLite(element).data('$isolateScopeNoTemplate');
return jqLite.data(element, '$isolateScope') || jqLite.data(element, '$isolateScopeNoTemplate');
},

controller: jqLiteController,
Expand Down
2 changes: 1 addition & 1 deletion src/ng/compile.js
Expand Up @@ -970,7 +970,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
if (nodeLinkFn) {
if (nodeLinkFn.scope) {
childScope = scope.$new();
jqLite(node).data('$scope', childScope);
jqLite.data(node, '$scope', childScope);
} else {
childScope = scope;
}
Expand Down
22 changes: 22 additions & 0 deletions test/jqLiteSpec.js
Expand Up @@ -386,6 +386,7 @@ describe('jqLite', function() {
selected.removeData('prop2');
});


it('should add and remove data on SVGs', function() {
var svg = jqLite('<svg><rect></rect></svg>');

Expand Down Expand Up @@ -415,6 +416,27 @@ describe('jqLite', function() {
});


it('should provide the non-wrapped data calls', function() {
var node = document.createElement('div');

expect(jqLite.data(node, "foo")).toBeUndefined();

jqLite.data(node, "foo", "bar");

expect(jqLite.data(node, "foo")).toBe("bar");
expect(jqLite(node).data("foo")).toBe("bar");

expect(jqLite.data(node)).toBe(jqLite(node).data());

jqLite.removeData(node, "foo");
expect(jqLite.data(node, "foo")).toBeUndefined();

jqLite.data(node, "bar", "baz");
jqLite.removeData(node);
jqLite.removeData(node);
expect(jqLite.data(node, "bar")).toBeUndefined();
});

it('should emit $destroy event if element removed via remove()', function() {
var log = '';
var element = jqLite(a);
Expand Down

0 comments on commit e4ba894

Please sign in to comment.