Skip to content

Commit 0499c47

Browse files
committed
added ng:switch-when-default; changed $watch to always fire on init. (may be backward incompatible)
1 parent 43a4ff4 commit 0499c47

File tree

7 files changed

+66
-27
lines changed

7 files changed

+66
-27
lines changed

src/Scope.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ function createScope(parent, providers, instanceCache) {
167167

168168
$watch: function(watchExp, listener, exceptionHandler) {
169169
var watch = expressionCompile(watchExp),
170-
last;
170+
last = {};
171171
listener = expressionCompile(listener);
172172
function watcher(){
173173
var value = watch.call(instance),

src/directives.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,9 +622,9 @@ angularDirective("ng:submit", function(expression, element) {
622622
</div>
623623
* @scenario
624624
it('should check ng:watch', function(){
625-
expect(using('.doc-example-live').binding('counter')).toBe('1');
626-
using('.doc-example-live').input('name').enter('abc');
627625
expect(using('.doc-example-live').binding('counter')).toBe('2');
626+
using('.doc-example-live').input('name').enter('abc');
627+
expect(using('.doc-example-live').binding('counter')).toBe('3');
628628
});
629629
*/
630630
angularDirective("ng:watch", function(expression, element){

src/widgets.js

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -462,28 +462,31 @@ angularWidget('ng:include', function(element){
462462
* Conditionally change the DOM structure.
463463
*
464464
* @usageContent
465-
* <any ng:switch-when="matchValue1"/>...</any>
466-
* <any ng:switch-when="matchValue2"/>...</any>
465+
* <any ng:switch-when="matchValue1">...</any>
466+
* <any ng:switch-when="matchValue2">...</any>
467467
* ...
468-
* <any ng:switch-when="matchValueN"/>...</any>
468+
* <any ng:switch-default>...</any>
469469
*
470470
* @param {*} on expression to match against <tt>ng:switch-when</tt>.
471471
* @paramDescription
472472
* On child elments add:
473473
*
474474
* * `ng:switch-when`: the case statement to match against. If match then this
475475
* case will be displayed.
476+
* * `ng:switch-default`: the default case when no other casses match.
476477
*
477478
* @example
478479
<select name="switch">
479480
<option>settings</option>
480481
<option>home</option>
482+
<option>other</option>
481483
</select>
482484
<tt>switch={{switch}}</tt>
483485
</hr>
484486
<ng:switch on="switch" >
485487
<div ng:switch-when="settings">Settings Div</div>
486488
<span ng:switch-when="home">Home Span</span>
489+
<span ng:switch-default>default</span>
487490
</ng:switch>
488491
</code>
489492
*
@@ -495,6 +498,10 @@ angularWidget('ng:include', function(element){
495498
* select('switch').option('home');
496499
* expect(element('.doc-example ng\\:switch').text()).toEqual('Home Span');
497500
* });
501+
* it('should select deafault', function(){
502+
* select('switch').option('other');
503+
* expect(element('.doc-example ng\\:switch').text()).toEqual('default');
504+
* });
498505
*/
499506
var ngSwitch = angularWidget('ng:switch', function (element){
500507
var compiler = this,
@@ -505,21 +512,26 @@ var ngSwitch = angularWidget('ng:switch', function (element){
505512
changeExpr = element.attr('change') || '',
506513
cases = [];
507514
if (!usingFn) throw "Using expression '" + usingExpr + "' unknown.";
515+
if (!watchExpr) throw "Missing 'on' attribute.";
508516
eachNode(element, function(caseElement){
509517
var when = caseElement.attr('ng:switch-when');
510-
if (when) {
511-
cases.push({
512-
when: function(scope, value){
513-
var args = [value, when];
514-
foreach(usingExprParams, function(arg){
515-
args.push(arg);
516-
});
517-
return usingFn.apply(scope, args);
518-
},
518+
var switchCase = {
519519
change: changeExpr,
520520
element: caseElement,
521521
template: compiler.compile(caseElement)
522-
});
522+
};
523+
if (isString(when)) {
524+
switchCase.when = function(scope, value){
525+
var args = [value, when];
526+
foreach(usingExprParams, function(arg){
527+
args.push(arg);
528+
});
529+
return usingFn.apply(scope, args);
530+
};
531+
cases.unshift(switchCase);
532+
} else if (isString(caseElement.attr('ng:switch-default'))) {
533+
switchCase.when = valueFn(true);
534+
cases.push(switchCase);
523535
}
524536
});
525537

@@ -532,10 +544,12 @@ var ngSwitch = angularWidget('ng:switch', function (element){
532544
return function(element){
533545
var scope = this, childScope;
534546
this.$watch(watchExpr, function(value){
547+
var found = false;
535548
element.html('');
536549
childScope = createScope(scope);
537550
foreach(cases, function(switchCase){
538-
if (switchCase.when(childScope, value)) {
551+
if (!found && switchCase.when(childScope, value)) {
552+
found = true;
539553
var caseElement = quickClone(switchCase.element);
540554
element.append(caseElement);
541555
childScope.$tryEval(switchCase.change, element);
@@ -550,7 +564,7 @@ var ngSwitch = angularWidget('ng:switch', function (element){
550564
};
551565
}, {
552566
equals: function(on, when) {
553-
return on == when;
567+
return ''+on == when;
554568
},
555569
route: switchRouteMatcher
556570
});

test/BinderTest.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -607,13 +607,13 @@ BinderTest.prototype.testItShouldListenOnRightScope = function() {
607607
'<ul ng:init="counter=0; gCounter=0" ng:watch="w:counter=counter+1">' +
608608
'<li ng:repeat="n in [1,2,4]" ng:watch="w:counter=counter+1;w:$root.gCounter=$root.gCounter+n"/></ul>');
609609
c.scope.$eval();
610-
assertEquals(0, c.scope.$get("counter"));
611-
assertEquals(0, c.scope.$get("gCounter"));
610+
assertEquals(1, c.scope.$get("counter"));
611+
assertEquals(7, c.scope.$get("gCounter"));
612612

613613
c.scope.$set("w", "something");
614614
c.scope.$eval();
615-
assertEquals(1, c.scope.$get("counter"));
616-
assertEquals(7, c.scope.$get("gCounter"));
615+
assertEquals(2, c.scope.$get("counter"));
616+
assertEquals(14, c.scope.$get("gCounter"));
617617
};
618618

619619
BinderTest.prototype.testItShouldRepeatOnHashes = function() {

test/CompilerSpec.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ describe('compiler', function(){
1414
watch: function(expression, element){
1515
return function() {
1616
this.$watch(expression, function(val){
17-
log += ":" + val;
17+
if (val)
18+
log += ":" + val;
1819
});
1920
};
2021
}

test/directivesSpec.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,12 @@ describe("directives", function(){
172172
var scope = compile('<div ng:watch="i: count = count + 1" ng:init="count = 0">');
173173
scope.$eval();
174174
scope.$eval();
175-
expect(scope.$get('count')).toEqual(0);
175+
expect(scope.$get('count')).toEqual(1);
176176

177177
scope.$set('i', 0);
178178
scope.$eval();
179179
scope.$eval();
180-
expect(scope.$get('count')).toEqual(1);
180+
expect(scope.$get('count')).toEqual(2);
181181
});
182182

183183
describe('ng:click', function(){

test/widgetsSpec.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,11 @@ describe("widget", function(){
430430

431431
describe('ng:switch', function(){
432432
it('should switch on value change', function(){
433-
compile('<ng:switch on="select"><div ng:switch-when="1">first:{{name}}</div><div ng:switch-when="2">second:{{name}}</div></ng:switch>');
433+
compile('<ng:switch on="select">' +
434+
'<div ng:switch-when="1">first:{{name}}</div>' +
435+
'<div ng:switch-when="2">second:{{name}}</div>' +
436+
'<div ng:switch-when="true">true:{{name}}</div>' +
437+
'</ng:switch>');
434438
expect(element.html()).toEqual('');
435439
scope.select = 1;
436440
scope.$eval();
@@ -444,8 +448,28 @@ describe("widget", function(){
444448
scope.name = 'misko';
445449
scope.$eval();
446450
expect(element.text()).toEqual('second:misko');
451+
scope.select = true;
452+
scope.$eval();
453+
expect(element.text()).toEqual('true:misko');
454+
});
455+
456+
it("should compare stringified versions", function(){
457+
var switchWidget = angular.widget('ng:switch');
458+
expect(switchWidget.equals(true, 'true')).toEqual(true);
447459
});
448460

461+
it('should switch on switch-when-default', function(){
462+
compile('<ng:switch on="select">' +
463+
'<div ng:switch-when="1">one</div>' +
464+
'<div ng:switch-default>other</div>' +
465+
'</ng:switch>');
466+
scope.$eval();
467+
expect(element.text()).toEqual('other');
468+
scope.select = 1;
469+
scope.$eval();
470+
expect(element.text()).toEqual('one');
471+
});
472+
449473
it("should match urls", function(){
450474
var scope = angular.compile('<ng:switch on="url" using="route:params"><div ng:switch-when="/Book/:name">{{params.name}}</div></ng:switch>');
451475
scope.url = '/Book/Moby';
@@ -459,7 +483,7 @@ describe("widget", function(){
459483
expect(match).toBeFalsy();
460484
});
461485

462-
it('should call init on switch', function(){
486+
it('should call change on switch', function(){
463487
var scope = angular.compile('<ng:switch on="url" change="name=\'works\'"><div ng:switch-when="a">{{name}}</div></ng:switch>');
464488
var cleared = false;
465489
scope.url = 'a';

0 commit comments

Comments
 (0)