Skip to content

Commit e6d5c34

Browse files
committed
feat(change_detection): add static append method to Pipes
This change allows creation of a new Pipes object with new pipes appended to pipes of an inherited Pipes. Closes angular#2901
1 parent 83d2a81 commit e6d5c34

File tree

2 files changed

+128
-4
lines changed

2 files changed

+128
-4
lines changed

modules/angular2/src/change_detection/pipes/pipes.ts

+81-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
1-
import {List, ListWrapper} from 'angular2/src/facade/collection';
1+
import {ListWrapper, isListLikeIterable, StringMapWrapper} from 'angular2/src/facade/collection';
22
import {isBlank, isPresent, BaseException, CONST} from 'angular2/src/facade/lang';
33
import {Pipe, PipeFactory} from './pipe';
4-
import {Injectable} from 'angular2/src/di/decorators';
4+
import {Injectable, UnboundedMetadata, OptionalMetadata} from 'angular2/di';
55
import {ChangeDetectorRef} from '../change_detector_ref';
6+
import {Binding} from 'angular2/di';
67

78
@Injectable()
89
@CONST()
910
export class Pipes {
10-
constructor(public config) {}
11+
/**
12+
* Map of {@link Pipe} names to {@link PipeFactory} lists used to configure the
13+
* {@link Pipes} registry.
14+
*
15+
* #Example
16+
*
17+
* ```
18+
* var pipesConfig = {
19+
* 'json': [jsonPipeFactory]
20+
* }
21+
* @Component({
22+
* viewInjector: [
23+
* bind(Pipes).toValue(new Pipes(pipesConfig))
24+
* ]
25+
* })
26+
* ```
27+
*/
28+
config: StringMap<string, PipeFactory[]>;
29+
constructor(config: StringMap<string, PipeFactory[]>) { this.config = config; }
1130

1231
get(type: string, obj, cdRef?: ChangeDetectorRef, existingPipe?: Pipe): Pipe {
1332
if (isPresent(existingPipe) && existingPipe.supports(obj)) return existingPipe;
@@ -20,6 +39,65 @@ export class Pipes {
2039
return factory.create(cdRef);
2140
}
2241

42+
/**
43+
* Takes a {@link Pipes} config object and returns a binding used to append the
44+
* provided config to an inherited {@link Pipes} instance and return a new
45+
* {@link Pipes} instance.
46+
*
47+
* If the provided config contains a key that is not yet present in the
48+
* inherited {@link Pipes}' config, a new {@link PipeFactory} list will be created
49+
* for that key. Otherwise, the provided config will be merged with the inherited
50+
* {@link Pipes} instance by appending pipes to their respective keys, without mutating
51+
* the inherited {@link Pipes}.
52+
*
53+
* The following example shows how to append a new {@link PipeFactory} to the
54+
* existing list of `async` factories, which will only be applied to the injector
55+
* for this component and its children. This step is all that's required to make a new
56+
* pipe available to this component's template.
57+
*
58+
* # Example
59+
*
60+
* ```
61+
* @Component({
62+
* viewInjector: [
63+
* Pipes.append({
64+
* async: [newAsyncPipe]
65+
* })
66+
* ]
67+
* })
68+
* ```
69+
*/
70+
static append(config): Binding {
71+
return new Binding(Pipes, {
72+
toFactory: (pipes: Pipes) => {
73+
if (!isPresent(pipes)) {
74+
// Typically would occur when calling Pipe.append inside of dependencies passed to
75+
// bootstrap(), which would override default pipes instead of append.
76+
throw new BaseException('Cannot append to Pipes without a parent injector');
77+
}
78+
var mergedConfig: StringMap<string, PipeFactory[]> = <StringMap<string, PipeFactory[]>>{};
79+
80+
// Manual deep copy of existing Pipes config,
81+
// so that lists of PipeFactories don't get mutated.
82+
StringMapWrapper.forEach(pipes.config, (v: PipeFactory[], k: string) => {
83+
var localPipeList: PipeFactory[] = mergedConfig[k] = [];
84+
v.forEach((p: PipeFactory) => { localPipeList.push(p); });
85+
});
86+
87+
StringMapWrapper.forEach(config, (v: PipeFactory[], k: string) => {
88+
if (isListLikeIterable(mergedConfig[k])) {
89+
mergedConfig[k] = ListWrapper.concat(mergedConfig[k], config[k]);
90+
} else {
91+
mergedConfig[k] = config[k];
92+
}
93+
});
94+
return new Pipes(mergedConfig);
95+
},
96+
// Dependency technically isn't optional, but we can provide a better error message this way.
97+
deps: [[Pipes, new UnboundedMetadata(), new OptionalMetadata()]]
98+
})
99+
}
100+
23101
private _getListOfFactories(type: string, obj: any): PipeFactory[] {
24102
var listOfFactories = this.config[type];
25103
if (isBlank(listOfFactories)) {

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

+47-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import {
1111
SpyPipeFactory
1212
} from 'angular2/test_lib';
1313

14+
import {Injector, bind} from 'angular2/di';
1415
import {Pipes} from 'angular2/src/change_detection/pipes/pipes';
16+
import {PipeFactory} from 'angular2/src/change_detection/pipes/pipe';
1517

1618
export function main() {
1719
describe("pipe registry", () => {
@@ -73,5 +75,49 @@ export function main() {
7375
expect(() => r.get("type", "some object"))
7476
.toThrowError(`Cannot find 'type' pipe supporting object 'some object'`);
7577
});
78+
79+
describe('.append()', () => {
80+
it('should create a factory that appends new pipes to old', () => {
81+
firstPipeFactory.spy("supports").andReturn(false);
82+
secondPipeFactory.spy("supports").andReturn(true);
83+
secondPipeFactory.spy("create").andReturn(secondPipe);
84+
var originalPipes: Pipes = new Pipes({'async': [firstPipeFactory]});
85+
var binding = Pipes.append({'async':<PipeFactory[]>[secondPipeFactory]});
86+
var pipes: Pipes = binding.toFactory(originalPipes);
87+
88+
expect(pipes.config['async'].length).toBe(2);
89+
expect(originalPipes.config['async'].length).toBe(1);
90+
expect(pipes.get('async', 'second plz')).toBe(secondPipe);
91+
});
92+
93+
94+
it('should append to di-inherited pipes', () => {
95+
firstPipeFactory.spy("supports").andReturn(false);
96+
secondPipeFactory.spy("supports").andReturn(true);
97+
secondPipeFactory.spy("create").andReturn(secondPipe);
98+
99+
var originalPipes: Pipes = new Pipes({'async': [firstPipeFactory]});
100+
var injector: Injector = Injector.resolveAndCreate([bind(Pipes).toValue(originalPipes)]);
101+
var childInjector: Injector =
102+
injector.resolveAndCreateChild([Pipes.append({'async': [secondPipeFactory]})]);
103+
var parentPipes: Pipes = injector.get(Pipes);
104+
var childPipes: Pipes = childInjector.get(Pipes);
105+
expect(childPipes.config['async'].length).toBe(2);
106+
expect(parentPipes.config['async'].length).toBe(1);
107+
expect(childPipes.get('async', 'second plz')).toBe(secondPipe);
108+
});
109+
110+
111+
it('should throw if calling append when creating root injector', () => {
112+
secondPipeFactory.spy("supports").andReturn(true);
113+
secondPipeFactory.spy("create").andReturn(secondPipe);
114+
115+
var injector: Injector =
116+
Injector.resolveAndCreate([Pipes.append({'async': [secondPipeFactory]})]);
117+
118+
expect(() => injector.get(Pipes))
119+
.toThrowError(/Cannot append to Pipes without a parent injector/g);
120+
});
121+
});
76122
});
77-
}
123+
}

0 commit comments

Comments
 (0)