Skip to content

Commit 5306b6d

Browse files
fix(change_detection): expose values when detecting changes in key-value pairs
Fixes angular#1118 Closes angular#1123
1 parent b096240 commit 5306b6d

File tree

2 files changed

+33
-15
lines changed

2 files changed

+33
-15
lines changed

modules/angular2/src/change_detection/pipes/keyvalue_changes.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ export class KeyValueChanges extends Pipe {
107107
var newSeqRecord;
108108
if (oldSeqRecord !== null && key === oldSeqRecord.key) {
109109
newSeqRecord = oldSeqRecord;
110-
if (!looseIdentical(value, oldSeqRecord._currentValue)) {
111-
oldSeqRecord._previousValue = oldSeqRecord._currentValue;
112-
oldSeqRecord._currentValue = value;
110+
if (!looseIdentical(value, oldSeqRecord.currentValue)) {
111+
oldSeqRecord.previousValue = oldSeqRecord.currentValue;
112+
oldSeqRecord.currentValue = value;
113113
this._addToChanges(oldSeqRecord);
114114
}
115115
} else {
@@ -124,7 +124,7 @@ export class KeyValueChanges extends Pipe {
124124
} else {
125125
newSeqRecord = new KVChangeRecord(key);
126126
MapWrapper.set(records, key, newSeqRecord);
127-
newSeqRecord._currentValue = value;
127+
newSeqRecord.currentValue = value;
128128
this._addToAdditions(newSeqRecord);
129129
}
130130
}
@@ -158,11 +158,11 @@ export class KeyValueChanges extends Pipe {
158158
}
159159

160160
for (record = this._changesHead; record !== null; record = record._nextChanged) {
161-
record._previousValue = record._currentValue;
161+
record.previousValue = record.currentValue;
162162
}
163163

164164
for (record = this._additionsHead; record != null; record = record._nextAdded) {
165-
record._previousValue = record._currentValue;
165+
record.previousValue = record.currentValue;
166166
}
167167

168168
// todo(vicb) once assert is supported
@@ -215,8 +215,8 @@ export class KeyValueChanges extends Pipe {
215215
}
216216

217217
for (var rec:KVChangeRecord = this._removalsHead; rec !== null; rec = rec._nextRemoved) {
218-
rec._previousValue = rec._currentValue;
219-
rec._currentValue = null;
218+
rec.previousValue = rec.currentValue;
219+
rec.currentValue = null;
220220
MapWrapper.delete(this._records, rec.key);
221221
}
222222
}
@@ -351,8 +351,8 @@ export class KeyValueChanges extends Pipe {
351351

352352
export class KVChangeRecord {
353353
key;
354-
_previousValue;
355-
_currentValue;
354+
previousValue;
355+
currentValue;
356356

357357
_nextPrevious:KVChangeRecord;
358358
_next:KVChangeRecord;
@@ -363,8 +363,8 @@ export class KVChangeRecord {
363363

364364
constructor(key) {
365365
this.key = key;
366-
this._previousValue = null;
367-
this._currentValue = null;
366+
this.previousValue = null;
367+
this.currentValue = null;
368368

369369
this._nextPrevious = null;
370370
this._next = null;
@@ -375,9 +375,9 @@ export class KVChangeRecord {
375375
}
376376

377377
toString():string {
378-
return looseIdentical(this._previousValue, this._currentValue) ?
378+
return looseIdentical(this.previousValue, this.currentValue) ?
379379
stringify(this.key) :
380-
(stringify(this.key) + '[' + stringify(this._previousValue) + '->' +
381-
stringify(this._currentValue) + ']');
380+
(stringify(this.key) + '[' + stringify(this.previousValue) + '->' +
381+
stringify(this.currentValue) + ']');
382382
}
383383
}

modules/angular2/test/change_detection/keyvalue_changes_spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@ export function main() {
5454
}));
5555
});
5656

57+
it('should expose previous and current value', () => {
58+
var previous, current;
59+
60+
MapWrapper.set(m, 1, 10);
61+
changes.check(m);
62+
63+
MapWrapper.set(m, 1, 20);
64+
changes.check(m);
65+
66+
changes.forEachChangedItem((record) => {
67+
previous = record.previousValue;
68+
current = record.currentValue;
69+
})
70+
71+
expect(previous).toEqual(10);
72+
expect(current).toEqual(20);
73+
});
74+
5775
it('should do basic map watching', () => {
5876
changes.check(m);
5977

0 commit comments

Comments
 (0)