mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-16 23:30:23 +00:00
perf(a): do not link when href or name exists in template
Change the a directive to link and hookup a click event only when there is no href or name in the template element. In a large Google app, this results in about 800 fewer registrations, saving a small but measurable amount of time and memory. Closes #5362
This commit is contained in:
parent
fcd2a8131a
commit
f3de5b6eac
2 changed files with 36 additions and 8 deletions
|
|
@ -32,13 +32,15 @@ var htmlAnchorDirective = valueFn({
|
|||
element.append(document.createComment('IE fix'));
|
||||
}
|
||||
|
||||
return function(scope, element) {
|
||||
element.on('click', function(event){
|
||||
// if we have no href url, then don't navigate anywhere.
|
||||
if (!element.attr('href')) {
|
||||
event.preventDefault();
|
||||
}
|
||||
});
|
||||
};
|
||||
if (!attr.href && !attr.name) {
|
||||
return function(scope, element) {
|
||||
element.on('click', function(event){
|
||||
// if we have no href url, then don't navigate anywhere.
|
||||
if (!element.attr('href')) {
|
||||
event.preventDefault();
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -58,4 +58,30 @@ describe('a', function() {
|
|||
|
||||
expect(element.text()).toBe('hello@you');
|
||||
});
|
||||
|
||||
|
||||
it('should not link and hookup an event if href is present at compile', function() {
|
||||
var jq = jQuery || jqLite;
|
||||
element = jq('<a href="//a.com">hello@you</a>');
|
||||
var linker = $compile(element);
|
||||
|
||||
spyOn(jq.prototype, 'on');
|
||||
|
||||
linker($rootScope);
|
||||
|
||||
expect(jq.prototype.on).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
|
||||
it('should not link and hookup an event if name is present at compile', function() {
|
||||
var jq = jQuery || jqLite;
|
||||
element = jq('<a name="bobby">hello@you</a>');
|
||||
var linker = $compile(element);
|
||||
|
||||
spyOn(jq.prototype, 'on');
|
||||
|
||||
linker($rootScope);
|
||||
|
||||
expect(jq.prototype.on).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue