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 +++++++++++++++++++++++++++++--------------- 3 files changed, 32 insertions(+), 18 deletions(-) (limited to 'src') 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 }); -- cgit v1.2.3