fix(change_detection): pass the correct previous value when using pipes
Closes #588
This commit is contained in:
parent
1d4ffd986d
commit
7f31036427
|
@ -157,11 +157,11 @@ export class DynamicChangeDetector extends AbstractChangeDetector {
|
||||||
|
|
||||||
var newValue = pipe.transform(context);
|
var newValue = pipe.transform(context);
|
||||||
if (! ChangeDetectionUtil.noChangeMarker(newValue)) {
|
if (! ChangeDetectionUtil.noChangeMarker(newValue)) {
|
||||||
|
var prevValue = this._readSelf(proto);
|
||||||
this._writeSelf(proto, newValue);
|
this._writeSelf(proto, newValue);
|
||||||
this._setChanged(proto, true);
|
this._setChanged(proto, true);
|
||||||
|
|
||||||
if (proto.lastInBinding) {
|
if (proto.lastInBinding) {
|
||||||
var prevValue = this._readSelf(proto);
|
|
||||||
return ChangeDetectionUtil.simpleChange(prevValue, newValue);
|
return ChangeDetectionUtil.simpleChange(prevValue, newValue);
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
|
|
|
@ -7,8 +7,9 @@ import {Parser} from 'angular2/src/change_detection/parser/parser';
|
||||||
import {Lexer} from 'angular2/src/change_detection/parser/lexer';
|
import {Lexer} from 'angular2/src/change_detection/parser/lexer';
|
||||||
|
|
||||||
import {ChangeDispatcher, DynamicChangeDetector, ChangeDetectionError, ContextWithVariableBindings,
|
import {ChangeDispatcher, DynamicChangeDetector, ChangeDetectionError, ContextWithVariableBindings,
|
||||||
PipeRegistry, NO_CHANGE,
|
PipeRegistry, NO_CHANGE, CHECK_ALWAYS, CHECK_ONCE, CHECKED, DETACHED} from 'angular2/change_detection';
|
||||||
CHECK_ALWAYS, CHECK_ONCE, CHECKED, DETACHED} from 'angular2/change_detection';
|
|
||||||
|
import {ChangeDetectionUtil} from 'angular2/src/change_detection/change_detection_util';
|
||||||
|
|
||||||
|
|
||||||
import {JitProtoChangeDetector, DynamicProtoChangeDetector} from 'angular2/src/change_detection/proto_change_detector';
|
import {JitProtoChangeDetector, DynamicProtoChangeDetector} from 'angular2/src/change_detection/proto_change_detector';
|
||||||
|
@ -190,6 +191,43 @@ export function main() {
|
||||||
expect(dispatcher.log).toEqual(["memo=BvalueA"]);
|
expect(dispatcher.log).toEqual(["memo=BvalueA"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("change notification", () => {
|
||||||
|
describe("simple checks", () => {
|
||||||
|
it("should pass a change record to the dispatcher", () => {
|
||||||
|
var person = new Person('bob');
|
||||||
|
var c = createChangeDetector('name', 'name', person);
|
||||||
|
var cd = c["changeDetector"];
|
||||||
|
var dispatcher = c["dispatcher"];
|
||||||
|
|
||||||
|
cd.detectChanges();
|
||||||
|
|
||||||
|
var changeRecord = dispatcher.changeRecords[0][0];
|
||||||
|
|
||||||
|
expect(changeRecord.bindingMemento).toEqual('name');
|
||||||
|
expect(changeRecord.change.currentValue).toEqual('bob');
|
||||||
|
expect(changeRecord.change.previousValue).toEqual(ChangeDetectionUtil.unitialized());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("pipes", () => {
|
||||||
|
it("should pass a change record to the dispatcher", () => {
|
||||||
|
var registry = new FakePipeRegistry('pipe', () => new CountingPipe());
|
||||||
|
|
||||||
|
var person = new Person('bob');
|
||||||
|
var c = createChangeDetector('name', 'name | pipe', person, registry);
|
||||||
|
var cd = c["changeDetector"];
|
||||||
|
var dispatcher = c["dispatcher"];
|
||||||
|
|
||||||
|
cd.detectChanges();
|
||||||
|
|
||||||
|
var changeRecord = dispatcher.changeRecords[0][0];
|
||||||
|
|
||||||
|
expect(changeRecord.bindingMemento).toEqual('name');
|
||||||
|
expect(changeRecord.change.currentValue).toEqual('bob state:0');
|
||||||
|
expect(changeRecord.change.previousValue).toEqual(ChangeDetectionUtil.unitialized());
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("group changes", () => {
|
describe("group changes", () => {
|
||||||
it("should notify the dispatcher when a group of records changes", () => {
|
it("should notify the dispatcher when a group of records changes", () => {
|
||||||
var pcd = createProtoChangeDetector();
|
var pcd = createProtoChangeDetector();
|
||||||
|
@ -234,6 +272,7 @@ export function main() {
|
||||||
expect(dispatcher.loggedValues).toEqual(['InvokeA', ['a'], 'InvokeB', 'InvokeC', ['b', 'c']]);
|
expect(dispatcher.loggedValues).toEqual(['InvokeA', ['a'], 'InvokeB', 'InvokeC', ['b', 'c']]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe("enforce no new changes", () => {
|
describe("enforce no new changes", () => {
|
||||||
it("should throw when a record gets changed after it has been checked", () => {
|
it("should throw when a record gets changed after it has been checked", () => {
|
||||||
|
@ -576,6 +615,7 @@ class TestData {
|
||||||
class TestDispatcher extends ChangeDispatcher {
|
class TestDispatcher extends ChangeDispatcher {
|
||||||
log:List;
|
log:List;
|
||||||
loggedValues:List;
|
loggedValues:List;
|
||||||
|
changeRecords:List;
|
||||||
onChange:Function;
|
onChange:Function;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
|
@ -589,21 +629,24 @@ class TestDispatcher extends ChangeDispatcher {
|
||||||
clear() {
|
clear() {
|
||||||
this.log = ListWrapper.create();
|
this.log = ListWrapper.create();
|
||||||
this.loggedValues = ListWrapper.create();
|
this.loggedValues = ListWrapper.create();
|
||||||
|
this.changeRecords = ListWrapper.create();
|
||||||
}
|
}
|
||||||
|
|
||||||
logValue(value) {
|
logValue(value) {
|
||||||
ListWrapper.push(this.loggedValues, value);
|
ListWrapper.push(this.loggedValues, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
onRecordChange(group, updates:List) {
|
onRecordChange(group, changeRecords:List) {
|
||||||
var value = updates[0].change.currentValue;
|
var value = changeRecords[0].change.currentValue;
|
||||||
var memento = updates[0].bindingMemento;
|
var memento = changeRecords[0].bindingMemento;
|
||||||
ListWrapper.push(this.log, memento + '=' + this._asString(value));
|
ListWrapper.push(this.log, memento + '=' + this._asString(value));
|
||||||
|
|
||||||
var values = ListWrapper.map(updates, (r) => r.change.currentValue);
|
var values = ListWrapper.map(changeRecords, (r) => r.change.currentValue);
|
||||||
ListWrapper.push(this.loggedValues, values);
|
ListWrapper.push(this.loggedValues, values);
|
||||||
|
|
||||||
this.onChange(group, updates);
|
ListWrapper.push(this.changeRecords, changeRecords);
|
||||||
|
|
||||||
|
this.onChange(group, changeRecords);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue