From 0499c4727036446f5c8a5722bbd9c4018dae146f Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Wed, 10 Nov 2010 16:08:54 -0800 Subject: added ng:switch-when-default; changed $watch to always fire on init. (may be backward incompatible) --- src/Scope.js | 2 +- src/directives.js | 4 ++-- src/widgets.js | 44 +++++++++++++++++++++++++++++--------------- test/BinderTest.js | 8 ++++---- test/CompilerSpec.js | 3 ++- test/directivesSpec.js | 4 ++-- test/widgetsSpec.js | 28 ++++++++++++++++++++++++++-- 7 files changed, 66 insertions(+), 27 deletions(-) diff --git a/src/Scope.js b/src/Scope.js index 40192e16..75a33813 100644 --- a/src/Scope.js +++ b/src/Scope.js @@ -167,7 +167,7 @@ function createScope(parent, providers, instanceCache) { $watch: function(watchExp, listener, exceptionHandler) { var watch = expressionCompile(watchExp), - last; + last = {}; listener = expressionCompile(listener); function watcher(){ var value = watch.call(instance), diff --git a/src/directives.js b/src/directives.js index 7dcc7f39..24b85908 100644 --- a/src/directives.js +++ b/src/directives.js @@ -622,9 +622,9 @@ angularDirective("ng:submit", function(expression, element) { * @scenario it('should check ng:watch', function(){ - expect(using('.doc-example-live').binding('counter')).toBe('1'); - using('.doc-example-live').input('name').enter('abc'); expect(using('.doc-example-live').binding('counter')).toBe('2'); + using('.doc-example-live').input('name').enter('abc'); + expect(using('.doc-example-live').binding('counter')).toBe('3'); }); */ angularDirective("ng:watch", function(expression, element){ diff --git a/src/widgets.js b/src/widgets.js index 3666486d..e639e32b 100644 --- a/src/widgets.js +++ b/src/widgets.js @@ -462,10 +462,10 @@ angularWidget('ng:include', function(element){ * Conditionally change the DOM structure. * * @usageContent - * ... - * ... + * ... + * ... * ... - * ... + * ... * * @param {*} on expression to match against ng:switch-when. * @paramDescription @@ -473,17 +473,20 @@ angularWidget('ng:include', function(element){ * * * `ng:switch-when`: the case statement to match against. If match then this * case will be displayed. + * * `ng:switch-default`: the default case when no other casses match. * * @example switch={{switch}}
Settings Div
Home Span + default
* @@ -495,6 +498,10 @@ angularWidget('ng:include', function(element){ * select('switch').option('home'); * expect(element('.doc-example ng\\:switch').text()).toEqual('Home Span'); * }); + * it('should select deafault', function(){ + * select('switch').option('other'); + * expect(element('.doc-example ng\\:switch').text()).toEqual('default'); + * }); */ var ngSwitch = angularWidget('ng:switch', function (element){ var compiler = this, @@ -505,21 +512,26 @@ var ngSwitch = angularWidget('ng:switch', function (element){ changeExpr = element.attr('change') || '', cases = []; if (!usingFn) throw "Using expression '" + usingExpr + "' unknown."; + if (!watchExpr) throw "Missing 'on' attribute."; eachNode(element, function(caseElement){ var when = caseElement.attr('ng:switch-when'); - if (when) { - cases.push({ - when: function(scope, value){ - var args = [value, when]; - foreach(usingExprParams, function(arg){ - args.push(arg); - }); - return usingFn.apply(scope, args); - }, + var switchCase = { change: changeExpr, element: caseElement, template: compiler.compile(caseElement) - }); + }; + if (isString(when)) { + switchCase.when = function(scope, value){ + var args = [value, when]; + foreach(usingExprParams, function(arg){ + args.push(arg); + }); + return usingFn.apply(scope, args); + }; + cases.unshift(switchCase); + } else if (isString(caseElement.attr('ng:switch-default'))) { + switchCase.when = valueFn(true); + cases.push(switchCase); } }); @@ -532,10 +544,12 @@ var ngSwitch = angularWidget('ng:switch', function (element){ return function(element){ var scope = this, childScope; this.$watch(watchExpr, function(value){ + var found = false; element.html(''); childScope = createScope(scope); foreach(cases, function(switchCase){ - if (switchCase.when(childScope, value)) { + if (!found && switchCase.when(childScope, value)) { + found = true; var caseElement = quickClone(switchCase.element); element.append(caseElement); childScope.$tryEval(switchCase.change, element); @@ -550,7 +564,7 @@ var ngSwitch = angularWidget('ng:switch', function (element){ }; }, { equals: function(on, when) { - return on == when; + return ''+on == when; }, route: switchRouteMatcher }); diff --git a/test/BinderTest.js b/test/BinderTest.js index 06f0a449..71b2f6b6 100644 --- a/test/BinderTest.js +++ b/test/BinderTest.js @@ -607,13 +607,13 @@ BinderTest.prototype.testItShouldListenOnRightScope = function() { ''); c.scope.$eval(); - assertEquals(0, c.scope.$get("counter")); - assertEquals(0, c.scope.$get("gCounter")); + assertEquals(1, c.scope.$get("counter")); + assertEquals(7, c.scope.$get("gCounter")); c.scope.$set("w", "something"); c.scope.$eval(); - assertEquals(1, c.scope.$get("counter")); - assertEquals(7, c.scope.$get("gCounter")); + assertEquals(2, c.scope.$get("counter")); + assertEquals(14, c.scope.$get("gCounter")); }; BinderTest.prototype.testItShouldRepeatOnHashes = function() { diff --git a/test/CompilerSpec.js b/test/CompilerSpec.js index 780fd7cb..fa63ab77 100644 --- a/test/CompilerSpec.js +++ b/test/CompilerSpec.js @@ -14,7 +14,8 @@ describe('compiler', function(){ watch: function(expression, element){ return function() { this.$watch(expression, function(val){ - log += ":" + val; + if (val) + log += ":" + val; }); }; } diff --git a/test/directivesSpec.js b/test/directivesSpec.js index 2e5aa2a0..a3aa2481 100644 --- a/test/directivesSpec.js +++ b/test/directivesSpec.js @@ -172,12 +172,12 @@ describe("directives", function(){ var scope = compile('
'); scope.$eval(); scope.$eval(); - expect(scope.$get('count')).toEqual(0); + expect(scope.$get('count')).toEqual(1); scope.$set('i', 0); scope.$eval(); scope.$eval(); - expect(scope.$get('count')).toEqual(1); + expect(scope.$get('count')).toEqual(2); }); describe('ng:click', function(){ diff --git a/test/widgetsSpec.js b/test/widgetsSpec.js index 2d1aef1a..34ea6b1d 100644 --- a/test/widgetsSpec.js +++ b/test/widgetsSpec.js @@ -430,7 +430,11 @@ describe("widget", function(){ describe('ng:switch', function(){ it('should switch on value change', function(){ - compile('
first:{{name}}
second:{{name}}
'); + compile('' + + '
first:{{name}}
' + + '
second:{{name}}
' + + '
true:{{name}}
' + + '
'); expect(element.html()).toEqual(''); scope.select = 1; scope.$eval(); @@ -444,8 +448,28 @@ describe("widget", function(){ scope.name = 'misko'; scope.$eval(); expect(element.text()).toEqual('second:misko'); + scope.select = true; + scope.$eval(); + expect(element.text()).toEqual('true:misko'); + }); + + it("should compare stringified versions", function(){ + var switchWidget = angular.widget('ng:switch'); + expect(switchWidget.equals(true, 'true')).toEqual(true); }); + it('should switch on switch-when-default', function(){ + compile('' + + '
one
' + + '
other
' + + '
'); + scope.$eval(); + expect(element.text()).toEqual('other'); + scope.select = 1; + scope.$eval(); + expect(element.text()).toEqual('one'); + }); + it("should match urls", function(){ var scope = angular.compile('
{{params.name}}
'); scope.url = '/Book/Moby'; @@ -459,7 +483,7 @@ describe("widget", function(){ expect(match).toBeFalsy(); }); - it('should call init on switch', function(){ + it('should call change on switch', function(){ var scope = angular.compile('
{{name}}
'); var cleared = false; scope.url = 'a'; -- cgit v1.2.3