From 5d502c7ce3e7b58f3971c018691fc031568e8ccd Mon Sep 17 00:00:00 2001 From: Paulo Cesar Date: Mon, 29 Sep 2014 15:49:10 -0300 Subject: [PATCH 1/2] tests --- src/ng/compile.js | 4 +- test/ng/compileSpec.js | 90 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index e8f582a385d8..cc16cca6ac5c 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1541,9 +1541,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { try { linkFn = directive.compile($compileNode, templateAttrs, childTranscludeFn); if (isFunction(linkFn)) { - addLinkFns(null, linkFn, attrStart, attrEnd); + addLinkFns(null, angular.bind(directive, linkFn), attrStart, attrEnd); } else if (linkFn) { - addLinkFns(linkFn.pre, linkFn.post, attrStart, attrEnd); + addLinkFns(angular.bind(directive, linkFn.pre), angular.bind(directive, linkFn.post), attrStart, attrEnd); } } catch (e) { $exceptionHandler(e, startingTag($compileNode)); diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index ddde992ab454..238f3e03eb0c 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -190,6 +190,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('')($rootScope); + $compile('')($rootScope); + $compile('')($rootScope); + $compile('')($rootScope); + $compile('')($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' + ); + }); + }); }); From ea6944db4f64abec84cc47e6bd485abc7ce9e6f4 Mon Sep 17 00:00:00 2001 From: Paulo Cesar Date: Mon, 29 Sep 2014 07:06:24 -0300 Subject: [PATCH 2/2] squash! tests angular.bind => bind --- src/ng/compile.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index cc16cca6ac5c..d4fa21fdedc3 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1541,9 +1541,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) { try { linkFn = directive.compile($compileNode, templateAttrs, childTranscludeFn); if (isFunction(linkFn)) { - addLinkFns(null, angular.bind(directive, linkFn), attrStart, attrEnd); + addLinkFns(null, bind(directive, linkFn), attrStart, attrEnd); } else if (linkFn) { - addLinkFns(angular.bind(directive, linkFn.pre), angular.bind(directive, linkFn.post), attrStart, attrEnd); + addLinkFns(bind(directive, linkFn.pre), bind(directive, linkFn.post), attrStart, attrEnd); } } catch (e) { $exceptionHandler(e, startingTag($compileNode));