Skip to content

Commit e77710a

Browse files
PatrickJSvsavkin
authored andcommitted
fix(JsonPipe): always transform to json
BREAKING CHANGE: no longer cache ref
1 parent b6e95bb commit e77710a

File tree

3 files changed

+24
-46
lines changed

3 files changed

+24
-46
lines changed

modules/angular2/src/change_detection/change_detection.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import {DynamicProtoChangeDetector, JitProtoChangeDetector} from './proto_change_detector';
2-
import {PipeFactory} from './pipes/pipe';
2+
import {PipeFactory, Pipe} from './pipes/pipe';
33
import {PipeRegistry} from './pipes/pipe_registry';
44
import {IterableChangesFactory} from './pipes/iterable_changes';
55
import {KeyValueChangesFactory} from './pipes/keyvalue_changes';
66
import {ObservablePipeFactory} from './pipes/observable_pipe';
77
import {PromisePipeFactory} from './pipes/promise_pipe';
88
import {UpperCaseFactory} from './pipes/uppercase_pipe';
99
import {LowerCaseFactory} from './pipes/lowercase_pipe';
10-
import {JsonPipeFactory} from './pipes/json_pipe';
10+
import {JsonPipe} from './pipes/json_pipe';
1111
import {NullPipeFactory} from './pipes/null_pipe';
1212
import {ChangeDetection, ProtoChangeDetector, ChangeDetectorDefinition} from './interfaces';
1313
import {Injectable} from 'angular2/src/di/decorators';
@@ -55,7 +55,7 @@ export var lowercase: List<PipeFactory> = [new LowerCaseFactory(), new NullPipeF
5555
*
5656
* @exportedAs angular2/pipes
5757
*/
58-
export var json: List<PipeFactory> = [new JsonPipeFactory(), new NullPipeFactory()];
58+
export var json: List<PipeFactory | Pipe> = [new JsonPipe(), new NullPipeFactory()];
5959

6060
export var defaultPipes = {
6161
"iterableDiff": iterableDiff,
Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {isBlank, isPresent, CONST, Json} from 'angular2/src/facade/lang';
1+
import {isBlank, isPresent, Json} from 'angular2/src/facade/lang';
22
import {Pipe, PipeFactory} from './pipe';
33

44
/**
@@ -27,48 +27,9 @@ import {Pipe, PipeFactory} from './pipe';
2727
* @exportedAs angular2/pipes
2828
*/
2929
export class JsonPipe extends Pipe {
30-
_latestRef: any;
31-
_latestValue: any;
32-
constructor() {
33-
super();
34-
this._latestRef = null;
35-
this._latestValue = null;
36-
}
37-
38-
onDestroy(): void {
39-
if (isPresent(this._latestValue)) {
40-
this._latestRef = null;
41-
this._latestValue = null;
42-
}
43-
}
44-
4530
supports(obj): boolean { return true; }
4631

47-
transform(value): any {
48-
if (value === this._latestRef) {
49-
return this._latestValue;
50-
} else {
51-
return this._prettyPrint(value);
52-
}
53-
}
54-
55-
_prettyPrint(value) {
56-
this._latestRef = value;
57-
this._latestValue = Json.stringify(value);
58-
return this._latestValue;
59-
}
60-
}
61-
62-
/**
63-
* Provides a factory for [JsonPipeFactory].
64-
*
65-
* @exportedAs angular2/pipes
66-
*/
67-
@CONST()
68-
export class JsonPipeFactory extends PipeFactory {
69-
constructor() { super(); }
70-
71-
supports(obj): boolean { return true; }
32+
transform(value): string { return Json.stringify(value); }
7233

73-
create(cdRef): Pipe { return new JsonPipe(); }
34+
create(cdRef): Pipe { return this }
7435
}

modules/angular2/test/change_detection/pipes/json_pipe_spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
IS_DARTIUM
1515
} from 'angular2/test_lib';
1616
import {Json, RegExp, NumberWrapper, StringWrapper} from 'angular2/src/facade/lang';
17+
import {ListWrapper} from 'angular2/src/facade/collection';
1718

1819
import {JsonPipe} from 'angular2/src/change_detection/pipes/json_pipe';
1920

@@ -25,6 +26,7 @@ export function main() {
2526
var inceptionObjString;
2627
var catString;
2728
var pipe;
29+
var collection;
2830

2931
function normalize(obj: string): string { return StringWrapper.replace(obj, regNewLine, ''); }
3032

@@ -38,6 +40,7 @@ export function main() {
3840

3941
catString = 'Inception Cat';
4042
pipe = new JsonPipe();
43+
collection = [];
4144
});
4245

4346
describe("supports", () => {
@@ -72,11 +75,25 @@ export function main() {
7275
expect(dream1).toEqual(dream2);
7376
});
7477

75-
it("should return same value when nothing has changed since the last call", () => {
78+
it("should return same ref when nothing has changed since the last call", () => {
7679
expect(pipe.transform(inceptionObj)).toEqual(inceptionObjString);
7780
expect(pipe.transform(inceptionObj)).toEqual(inceptionObjString);
7881
});
7982

83+
84+
it("should return a new value when something changed but the ref hasn't", () => {
85+
var stringCollection = '[]';
86+
var stringCollectionWith1 = '[\n' +
87+
' 1' +
88+
'\n]';
89+
90+
expect(pipe.transform(collection)).toEqual(stringCollection);
91+
92+
ListWrapper.push(collection, 1);
93+
94+
expect(pipe.transform(collection)).toEqual(stringCollectionWith1);
95+
});
96+
8097
});
8198

8299
describe("onDestroy", () => {

0 commit comments

Comments
 (0)