added ng:switch-when-default; changed $watch to always fire on init. (may be backward incompatible)

This commit is contained in:
Misko Hevery
2010-11-10 16:08:54 -08:00
parent 43a4ff4cdf
commit 0499c47270
7 changed files with 66 additions and 27 deletions

View File

@@ -607,13 +607,13 @@ BinderTest.prototype.testItShouldListenOnRightScope = function() {
'<ul ng:init="counter=0; gCounter=0" ng:watch="w:counter=counter+1">' +
'<li ng:repeat="n in [1,2,4]" ng:watch="w:counter=counter+1;w:$root.gCounter=$root.gCounter+n"/></ul>');
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() {

View File

@@ -14,7 +14,8 @@ describe('compiler', function(){
watch: function(expression, element){
return function() {
this.$watch(expression, function(val){
log += ":" + val;
if (val)
log += ":" + val;
});
};
}

View File

@@ -172,12 +172,12 @@ describe("directives", function(){
var scope = compile('<div ng:watch="i: count = count + 1" ng:init="count = 0">');
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(){

View File

@@ -430,7 +430,11 @@ describe("widget", function(){
describe('ng:switch', function(){
it('should switch on value change', function(){
compile('<ng:switch on="select"><div ng:switch-when="1">first:{{name}}</div><div ng:switch-when="2">second:{{name}}</div></ng:switch>');
compile('<ng:switch on="select">' +
'<div ng:switch-when="1">first:{{name}}</div>' +
'<div ng:switch-when="2">second:{{name}}</div>' +
'<div ng:switch-when="true">true:{{name}}</div>' +
'</ng:switch>');
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('<ng:switch on="select">' +
'<div ng:switch-when="1">one</div>' +
'<div ng:switch-default>other</div>' +
'</ng:switch>');
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('<ng:switch on="url" using="route:params"><div ng:switch-when="/Book/:name">{{params.name}}</div></ng:switch>');
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('<ng:switch on="url" change="name=\'works\'"><div ng:switch-when="a">{{name}}</div></ng:switch>');
var cleared = false;
scope.url = 'a';