feat(pipe): added the Pipe decorator and the pipe property to View
BREAKING CHANGE:
Instead of configuring pipes via a Pipes object, now you can configure them by providing the pipes property to the View decorator.
@Pipe({
name: 'double'
})
class DoublePipe {
transform(value, args) { return value * 2; }
}
@View({
template: '{{ 10 | double}}'
pipes: [DoublePipe]
})
class CustomComponent {}
Closes #3572
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import {
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
xit,
|
||||
expect,
|
||||
beforeEach,
|
||||
afterEach
|
||||
} from 'angular2/test_lib';
|
||||
|
||||
import {PipeBinding} from 'angular2/src/core/pipes/pipe_binding';
|
||||
import {Pipe} from 'angular2/src/core/annotations_impl/annotations';
|
||||
|
||||
class MyPipe {}
|
||||
|
||||
export function main() {
|
||||
describe("PipeBinding", () => {
|
||||
it('should create a binding out of a type', () => {
|
||||
var binding = PipeBinding.createFromType(MyPipe, new Pipe({name: 'my-pipe'}));
|
||||
expect(binding.name).toEqual('my-pipe');
|
||||
expect(binding.factory()).toBeAnInstanceOf(MyPipe);
|
||||
expect(binding.dependencies.length).toEqual(0);
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import {
|
||||
ddescribe,
|
||||
xdescribe,
|
||||
describe,
|
||||
it,
|
||||
iit,
|
||||
xit,
|
||||
expect,
|
||||
beforeEach,
|
||||
afterEach
|
||||
} from 'angular2/test_lib';
|
||||
|
||||
import {PipeTransform} from 'angular2/change_detection';
|
||||
import {Injector, Inject, bind} from 'angular2/di';
|
||||
import {ProtoPipes, Pipes} from 'angular2/src/core/pipes/pipes';
|
||||
import {PipeBinding} from 'angular2/src/core/pipes/pipe_binding';
|
||||
import {Pipe} from 'angular2/src/core/annotations_impl/annotations';
|
||||
|
||||
class PipeA implements PipeTransform {
|
||||
transform(a, b) {}
|
||||
onDestroy() {}
|
||||
}
|
||||
|
||||
class PipeB implements PipeTransform {
|
||||
dep;
|
||||
constructor(@Inject("dep") dep: any) { this.dep = dep; }
|
||||
transform(a, b) {}
|
||||
onDestroy() {}
|
||||
}
|
||||
|
||||
export function main() {
|
||||
describe("Pipes", () => {
|
||||
var injector;
|
||||
|
||||
beforeEach(
|
||||
() => { injector = Injector.resolveAndCreate([bind('dep').toValue('dependency')]); });
|
||||
|
||||
it('should instantiate a pipe', () => {
|
||||
var proto = new ProtoPipes([PipeBinding.createFromType(PipeA, new Pipe({name: 'a'}))]);
|
||||
var pipes = new Pipes(proto, injector);
|
||||
expect(pipes.get("a")).toBeAnInstanceOf(PipeA);
|
||||
});
|
||||
|
||||
it('should throw when no pipe found', () => {
|
||||
var proto = new ProtoPipes([]);
|
||||
var pipes = new Pipes(proto, injector);
|
||||
expect(() => pipes.get("invalid")).toThrowErrorWith("Cannot find pipe 'invalid'");
|
||||
});
|
||||
|
||||
it('should inject dependencies from the provided injector', () => {
|
||||
var proto = new ProtoPipes([PipeBinding.createFromType(PipeB, new Pipe({name: 'b'}))]);
|
||||
var pipes = new Pipes(proto, injector);
|
||||
expect(pipes.get("b").dep).toEqual("dependency");
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user