fix(cd): report all changes on first cd run

- null values would not have been reported for Dart
- undefined values would not have been reported for JS

Closes #454
This commit is contained in:
Victor Berchet
2015-01-22 11:52:36 +01:00
committed by Misko Hevery
parent 829c28f3ee
commit b734d56b83
4 changed files with 38 additions and 6 deletions

View File

@ -21,6 +21,8 @@ import {
import {ChangeDetector, ChangeRecord, ChangeDispatcher} from './interfaces';
import {ExpressionChangedAfterItHasBeenChecked, ChangeDetectionError} from './exceptions';
var _uninitialized = new Object();
class SimpleChange {
previousValue:any;
currentValue:any;
@ -43,6 +45,7 @@ export class DynamicChangeDetector extends ChangeDetector {
this.dispatcher = dispatcher;
this.formatters = formatters;
this.values = ListWrapper.createFixedSize(protoRecords.length + 1);
ListWrapper.fill(this.values, _uninitialized);
this.protos = protoRecords;
this.children = [];
@ -123,10 +126,9 @@ export class DynamicChangeDetector extends ChangeDetector {
var prevValue = this._readSelf(proto);
var currValue = this._calculateCurrValue(proto);
if (! isSame(prevValue, currValue)) {
if (!isSame(prevValue, currValue)) {
this._writeSelf(proto, currValue);
return new SimpleChange(prevValue, currValue);
return new SimpleChange(prevValue === _uninitialized ? null : prevValue, currValue);
} else {
return null;
}
@ -174,7 +176,7 @@ export class DynamicChangeDetector extends ChangeDetector {
var self = this._readSelf(proto);
var context = this._readContext(proto);
if (isBlank(self)) {
if (isBlank(self) || self === _uninitialized) {
if (ArrayChanges.supports(context)) {
self = new ArrayChanges();
} else if (KeyValueChanges.supports(context)) {

View File

@ -57,6 +57,16 @@ export function main() {
expect(dispatcher.log).toEqual(['name=Misko']);
});
it('should report all changes on the first run including uninitialized values', () => {
var uninit = new Uninitialized();
var c = createChangeDetector('value', 'value', uninit);
var cd = c["changeDetector"];
var dispatcher = c["dispatcher"];
cd.detectChanges();
expect(dispatcher.log).toEqual(['value=null']);
});
it("should support literals", () => {
expect(executeWatch('const', '10')).toEqual(['const=10']);
expect(executeWatch('const', '"str"')).toEqual(['const=str']);
@ -237,10 +247,10 @@ export function main() {
pcd.addAst(ast('invalidProp', 'someComponent'), "a", 1);
var cd = pcd.instantiate(new TestDispatcher(), null);
cd.setContext(null);
try {
cd.detectChanges();
throw new BaseException("fail");
} catch (e) {
expect(e).toBeAnInstanceOf(ChangeDetectionError);
@ -430,6 +440,10 @@ class Address {
}
}
class Uninitialized {
value:any;
}
class TestData {
a;
@ -474,4 +488,4 @@ class TestDispatcher extends ChangeDispatcher {
_asString(value) {
return (isBlank(value) ? 'null' : value.toString());
}
}
}