From 9abd10e7b8a34b9dcd1a6af5ff37f57bd27cf920 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Thu, 15 Jul 2010 13:13:21 -0700 Subject: proper handlig of $element in filters --- src/Angular.js | 4 +++ src/directives.js | 18 ++++++++----- src/widgets.js | 3 +-- test/directivesSpec.js | 72 ++++++++++++++++++++++++++++++++------------------ 4 files changed, 63 insertions(+), 34 deletions(-) diff --git a/src/Angular.js b/src/Angular.js index 2b26c88d..07e9096b 100644 --- a/src/Angular.js +++ b/src/Angular.js @@ -78,6 +78,10 @@ function extend(dst) { return dst; } +function inherit(parent, extra) { + return extend(new (extend(function(){}, {prototype:parent}))(), extra); +}; + function noop() {} function identity($) {return $;} function extensionMap(angular, name) { diff --git a/src/directives.js b/src/directives.js index bcc427e8..b57e5ecd 100644 --- a/src/directives.js +++ b/src/directives.js @@ -26,12 +26,13 @@ angularDirective("ng:bind", function(expression){ return function(element) { var lastValue = noop, lastError = noop; this.$onEval(function() { - var error, - value = this.$tryEval(expression, function(e){ - error = toJson(e); - }), - isHtml, - isDomElement; + var error, value, isHtml, isDomElement, + oldElement = this.hasOwnProperty('$element') ? this.$element : undefined; + this.$element = element; + value = this.$tryEval(expression, function(e){ + error = toJson(e); + }); + this.$element = oldElement; if (lastValue === value && lastError == error) return; isHtml = value instanceof HTML, isDomElement = isElement(value); @@ -74,7 +75,9 @@ function compileBindTemplate(template){ }); }); bindTemplateCache[template] = fn = function(element){ - var parts = [], self = this; + var parts = [], self = this, + oldElement = this.hasOwnProperty('$element') ? this.$element : undefined; + this.$element = element; for ( var i = 0; i < bindings.length; i++) { var value = bindings[i].call(self, element); if (isElement(value)) @@ -83,6 +86,7 @@ function compileBindTemplate(template){ value = toJson(value, true); parts.push(value); }; + this.$element = oldElement; return parts.join(''); }; } diff --git a/src/widgets.js b/src/widgets.js index 0b77dbf4..7bd51c8c 100644 --- a/src/widgets.js +++ b/src/widgets.js @@ -83,8 +83,7 @@ function valueAccessor(scope, element) { elementError(element, NG_VALIDATION_ERROR, null); invalidWidgets.markValid(element); } else { - var error, - validateScope = extend(new (extend(function(){}, {prototype:scope}))(), {$element:element}); + var error, validateScope = inherit(scope, {$element:element}); error = required && !value ? 'Required' : (value ? validator(validateScope, value) : null); diff --git a/test/directivesSpec.js b/test/directivesSpec.js index a42faa9a..b6ccf764 100644 --- a/test/directivesSpec.js +++ b/test/directivesSpec.js @@ -29,35 +29,57 @@ describe("directives", function(){ expect(scope.a).toEqual(2); }); - it('should ng:bind', function() { - var scope = compile('
'); - expect(element.text()).toEqual(''); - scope.a = 'misko'; - scope.$eval(); - expect(element.text()).toEqual('misko'); - }); + describe('ng:bind', function(){ + it('should set text', function() { + var scope = compile(''); + expect(element.text()).toEqual(''); + scope.a = 'misko'; + scope.$eval(); + expect(element.text()).toEqual('misko'); + }); - it('should ng:bind html', function() { - var scope = compile(''); - scope.html = '