Skip to content
Permalink
Browse files

fix($compile): always use the DDO as `this` in pre-/post-linking func…

…tions

Closes #9306
  • Loading branch information
pocesar authored and gkalpak committed Sep 29, 2014
1 parent 10920a2 commit 47bc98a1eafd7853babc1a7dc54f13363a3f4a43
Showing with 92 additions and 2 deletions.
  1. +2 −2 src/ng/compile.js
  2. +90 −0 test/ng/compileSpec.js
@@ -2355,9 +2355,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
try {
linkFn = directive.compile($compileNode, templateAttrs, childTranscludeFn);
if (isFunction(linkFn)) {
addLinkFns(null, linkFn, attrStart, attrEnd);
addLinkFns(null, bind(directive, linkFn), attrStart, attrEnd);
} else if (linkFn) {
addLinkFns(linkFn.pre, linkFn.post, attrStart, attrEnd);
addLinkFns(bind(directive, linkFn.pre), bind(directive, linkFn.post), attrStart, attrEnd);
}
} catch (e) {
$exceptionHandler(e, startingTag($compileNode));
@@ -225,6 +225,96 @@ describe('$compile', function() {
});
inject(function($compile) {});
});

it('should preserve context within declaration', function() {
module(function() {
directive('ff', function(log) {
var declaration = {
restrict: 'E',
template: function(){
log('ff template: ' + (this === declaration));
},
compile: function(){
log('ff compile: ' + (this === declaration));
return function(){
log('ff post: ' + (this === declaration));
};
}
};
return declaration;
});

directive('fff', function(log) {
var declaration = {
restrict: 'E',
link: {
pre: function(){
log('fff pre: ' + (this === declaration));
},
post: function(){
log('fff post: ' + (this === declaration));
}
}
};
return declaration;
});

directive('ffff', function(log) {
var declaration = {
restrict: 'E',
compile: function(){
return {
pre: function(){
log('ffff pre: ' + (this === declaration));
},
post: function(){
log('ffff post: ' + (this === declaration));
}
};
}
};
return declaration;
});

directive('fffff', function(log) {
var declaration = {
restrict: 'E',
templateUrl: function(){
log('fffff: ' + (this === declaration));
}
};
return declaration;
});

directive('ffffff', function(log) {
var declaration = {
restrict: 'E',
link: function(){
log('ffffff: ' + (this === declaration));
}
};
return declaration;
});
});
inject(function($compile, $rootScope, log) {
$compile('<ff></ff>')($rootScope);
$compile('<fff></fff>')($rootScope);
$compile('<ffff></ffff>')($rootScope);
$compile('<fffff></fffff>')($rootScope);
$compile('<ffffff></ffffff>')($rootScope);
expect(log).toEqual(
'ff template: true; '+
'ff compile: true; '+
'ff post: true; '+
'fff pre: true; '+
'fff post: true; '+
'ffff pre: true; '+
'ffff post: true; '+
'fffff: true; '+
'ffffff: true'
);
});
});
});


0 comments on commit 47bc98a

Please sign in to comment.
You can’t perform that action at this time.