From 5306b6dd0c922f775868f455a8ff3ee5c04ff514 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Thu, 26 Mar 2015 19:19:10 +0100 Subject: [PATCH] fix(change_detection): expose values when detecting changes in key-value pairs Fixes #1118 Closes #1123 --- .../pipes/keyvalue_changes.js | 30 +++++++++---------- .../change_detection/keyvalue_changes_spec.js | 18 +++++++++++ 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/modules/angular2/src/change_detection/pipes/keyvalue_changes.js b/modules/angular2/src/change_detection/pipes/keyvalue_changes.js index fa3d04e1f2..0662ec359e 100644 --- a/modules/angular2/src/change_detection/pipes/keyvalue_changes.js +++ b/modules/angular2/src/change_detection/pipes/keyvalue_changes.js @@ -107,9 +107,9 @@ export class KeyValueChanges extends Pipe { var newSeqRecord; if (oldSeqRecord !== null && key === oldSeqRecord.key) { newSeqRecord = oldSeqRecord; - if (!looseIdentical(value, oldSeqRecord._currentValue)) { - oldSeqRecord._previousValue = oldSeqRecord._currentValue; - oldSeqRecord._currentValue = value; + if (!looseIdentical(value, oldSeqRecord.currentValue)) { + oldSeqRecord.previousValue = oldSeqRecord.currentValue; + oldSeqRecord.currentValue = value; this._addToChanges(oldSeqRecord); } } else { @@ -124,7 +124,7 @@ export class KeyValueChanges extends Pipe { } else { newSeqRecord = new KVChangeRecord(key); MapWrapper.set(records, key, newSeqRecord); - newSeqRecord._currentValue = value; + newSeqRecord.currentValue = value; this._addToAdditions(newSeqRecord); } } @@ -158,11 +158,11 @@ export class KeyValueChanges extends Pipe { } for (record = this._changesHead; record !== null; record = record._nextChanged) { - record._previousValue = record._currentValue; + record.previousValue = record.currentValue; } for (record = this._additionsHead; record != null; record = record._nextAdded) { - record._previousValue = record._currentValue; + record.previousValue = record.currentValue; } // todo(vicb) once assert is supported @@ -215,8 +215,8 @@ export class KeyValueChanges extends Pipe { } for (var rec:KVChangeRecord = this._removalsHead; rec !== null; rec = rec._nextRemoved) { - rec._previousValue = rec._currentValue; - rec._currentValue = null; + rec.previousValue = rec.currentValue; + rec.currentValue = null; MapWrapper.delete(this._records, rec.key); } } @@ -351,8 +351,8 @@ export class KeyValueChanges extends Pipe { export class KVChangeRecord { key; - _previousValue; - _currentValue; + previousValue; + currentValue; _nextPrevious:KVChangeRecord; _next:KVChangeRecord; @@ -363,8 +363,8 @@ export class KVChangeRecord { constructor(key) { this.key = key; - this._previousValue = null; - this._currentValue = null; + this.previousValue = null; + this.currentValue = null; this._nextPrevious = null; this._next = null; @@ -375,9 +375,9 @@ export class KVChangeRecord { } toString():string { - return looseIdentical(this._previousValue, this._currentValue) ? + return looseIdentical(this.previousValue, this.currentValue) ? stringify(this.key) : - (stringify(this.key) + '[' + stringify(this._previousValue) + '->' + - stringify(this._currentValue) + ']'); + (stringify(this.key) + '[' + stringify(this.previousValue) + '->' + + stringify(this.currentValue) + ']'); } } diff --git a/modules/angular2/test/change_detection/keyvalue_changes_spec.js b/modules/angular2/test/change_detection/keyvalue_changes_spec.js index 321cbdf1f8..22864005af 100644 --- a/modules/angular2/test/change_detection/keyvalue_changes_spec.js +++ b/modules/angular2/test/change_detection/keyvalue_changes_spec.js @@ -54,6 +54,24 @@ export function main() { })); }); + it('should expose previous and current value', () => { + var previous, current; + + MapWrapper.set(m, 1, 10); + changes.check(m); + + MapWrapper.set(m, 1, 20); + changes.check(m); + + changes.forEachChangedItem((record) => { + previous = record.previousValue; + current = record.currentValue; + }) + + expect(previous).toEqual(10); + expect(current).toEqual(20); + }); + it('should do basic map watching', () => { changes.check(m);