Skip to content

Commit 987a5fd

Browse files
committed
feat(change_detection): add support for pipes in the template
1 parent 29f5ee0 commit 987a5fd

File tree

12 files changed

+138
-134
lines changed

12 files changed

+138
-134
lines changed

modules/angular2/change_detection.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,33 @@ export var defaultPipes = {
3535
]
3636
};
3737

38-
var _registry = new PipeRegistry(defaultPipes);
39-
4038
export class DynamicChangeDetection extends ChangeDetection {
39+
registry:PipeRegistry;
40+
41+
constructor(registry:PipeRegistry) {
42+
super();
43+
this.registry = registry;
44+
}
45+
4146
createProtoChangeDetector(name:string):ProtoChangeDetector{
42-
return new DynamicProtoChangeDetector(_registry);
47+
return new DynamicProtoChangeDetector(this.registry);
4348
}
4449
}
4550

4651
export class JitChangeDetection extends ChangeDetection {
52+
registry:PipeRegistry;
53+
54+
constructor(registry:PipeRegistry) {
55+
super();
56+
this.registry = registry;
57+
}
58+
4759
createProtoChangeDetector(name:string):ProtoChangeDetector{
48-
return new JitProtoChangeDetector(_registry);
60+
return new JitProtoChangeDetector(this.registry);
4961
}
5062
}
5163

52-
export var dynamicChangeDetection = new DynamicChangeDetection();
53-
export var jitChangeDetection = new JitChangeDetection();
64+
var _registry = new PipeRegistry(defaultPipes);
65+
66+
export var dynamicChangeDetection = new DynamicChangeDetection(_registry);
67+
export var jitChangeDetection = new JitChangeDetection(_registry);

modules/angular2/src/change_detection/change_detection_jit_generator.es6

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import {
1414
RECORD_TYPE_INVOKE_CLOSURE,
1515
RECORD_TYPE_PRIMITIVE_OP,
1616
RECORD_TYPE_KEYED_ACCESS,
17-
RECORD_TYPE_INVOKE_FORMATTER,
1817
RECORD_TYPE_PIPE,
1918
RECORD_TYPE_INTERPOLATE
2019
} from './proto_record';
@@ -26,10 +25,9 @@ import {
2625
*
2726
* For example: An expression `address.city` will result in the following class:
2827
*
29-
* var ChangeDetector0 = function ChangeDetector0(dispatcher, formatters, protos) {
28+
* var ChangeDetector0 = function ChangeDetector0(dispatcher, protos) {
3029
* AbstractChangeDetector.call(this);
3130
* this.dispatcher = dispatcher;
32-
* this.formatters = formatters;
3331
* this.protos = protos;
3432
*
3533
* this.context = null;
@@ -89,31 +87,29 @@ import {
8987
var ABSTRACT_CHANGE_DETECTOR = "AbstractChangeDetector";
9088
var UTIL = "ChangeDetectionUtil";
9189
var DISPATCHER_ACCESSOR = "this.dispatcher";
92-
var FORMATTERS_ACCESSOR = "this.formatters";
90+
var PIPE_REGISTRY_ACCESSOR = "this.pipeRegistry";
9391
var PROTOS_ACCESSOR = "this.protos";
9492
var CHANGE_LOCAL = "change";
9593
var CHANGES_LOCAL = "changes";
9694
var TEMP_LOCAL = "temp";
97-
var PIPE_REGISTRY_ACCESSOR = "this.pipeRegistry";
9895

9996
function typeTemplate(type:string, cons:string, detectChanges:string, setContext:string):string {
10097
return `
10198
${cons}
10299
${detectChanges}
103100
${setContext};
104101
105-
return function(dispatcher, formatters, pipeRegistry) {
106-
return new ${type}(dispatcher, formatters, pipeRegistry, protos);
102+
return function(dispatcher, pipeRegistry) {
103+
return new ${type}(dispatcher, pipeRegistry, protos);
107104
}
108105
`;
109106
}
110107

111108
function constructorTemplate(type:string, fieldsDefinitions:string):string {
112109
return `
113-
var ${type} = function ${type}(dispatcher, formatters, pipeRegistry, protos) {
110+
var ${type} = function ${type}(dispatcher, pipeRegistry, protos) {
114111
${ABSTRACT_CHANGE_DETECTOR}.call(this);
115112
${DISPATCHER_ACCESSOR} = dispatcher;
116-
${FORMATTERS_ACCESSOR} = formatters;
117113
${PIPE_REGISTRY_ACCESSOR} = pipeRegistry;
118114
${PROTOS_ACCESSOR} = protos;
119115
${fieldsDefinitions}
@@ -381,9 +377,6 @@ export class ChangeDetectorJITGenerator {
381377
case RECORD_TYPE_INTERPOLATE:
382378
return assignmentTemplate(newValue, this.genInterpolation(r));
383379
384-
case RECORD_TYPE_INVOKE_FORMATTER:
385-
return assignmentTemplate(newValue, `${FORMATTERS_ACCESSOR}.get("${r.name}")(${args})`);
386-
387380
case RECORD_TYPE_KEYED_ACCESS:
388381
var key = this.localNames[r.args[0]];
389382
return assignmentTemplate(newValue, `${context}[${key}]`);

modules/angular2/src/change_detection/dynamic_change_detector.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import {
1616
RECORD_TYPE_INVOKE_CLOSURE,
1717
RECORD_TYPE_PRIMITIVE_OP,
1818
RECORD_TYPE_KEYED_ACCESS,
19-
RECORD_TYPE_INVOKE_FORMATTER,
2019
RECORD_TYPE_PIPE,
2120
RECORD_TYPE_INTERPOLATE
2221
} from './proto_record';
@@ -25,7 +24,6 @@ import {ExpressionChangedAfterItHasBeenChecked, ChangeDetectionError} from './ex
2524

2625
export class DynamicChangeDetector extends AbstractChangeDetector {
2726
dispatcher:any;
28-
formatters:Map;
2927
pipeRegistry;
3028

3129
values:List;
@@ -35,10 +33,9 @@ export class DynamicChangeDetector extends AbstractChangeDetector {
3533

3634
protos:List<ProtoRecord>;
3735

38-
constructor(dispatcher:any, formatters:Map, pipeRegistry:PipeRegistry, protoRecords:List<ProtoRecord>) {
36+
constructor(dispatcher:any, pipeRegistry:PipeRegistry, protoRecords:List<ProtoRecord>) {
3937
super();
4038
this.dispatcher = dispatcher;
41-
this.formatters = formatters;
4239
this.pipeRegistry = pipeRegistry;
4340

4441
this.values = ListWrapper.createFixedSize(protoRecords.length + 1);
@@ -149,10 +146,6 @@ export class DynamicChangeDetector extends AbstractChangeDetector {
149146
case RECORD_TYPE_PRIMITIVE_OP:
150147
return FunctionWrapper.apply(proto.funcOrValue, this._readArgs(proto));
151148

152-
case RECORD_TYPE_INVOKE_FORMATTER:
153-
var formatter = MapWrapper.get(this.formatters, proto.funcOrValue);
154-
return FunctionWrapper.apply(formatter, this._readArgs(proto));
155-
156149
default:
157150
throw new BaseException(`Unknown operation ${proto.mode}`);
158151
}

modules/angular2/src/change_detection/parser/ast.js

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -170,31 +170,15 @@ export class KeyedAccess extends AST {
170170
}
171171
}
172172

173-
export class Formatter extends AST {
173+
export class Pipe extends AST {
174174
exp:AST;
175175
name:string;
176176
args:List<AST>;
177-
allArgs:List<AST>;
178177
constructor(exp:AST, name:string, args:List) {
179178
super();
180179
this.exp = exp;
181180
this.name = name;
182181
this.args = args;
183-
this.allArgs = ListWrapper.concat([exp], args);
184-
}
185-
186-
visit(visitor) {
187-
return visitor.visitFormatter(this);
188-
}
189-
}
190-
191-
export class Pipe extends AST {
192-
exp:AST;
193-
name:string;
194-
constructor(exp:AST, name:string) {
195-
super();
196-
this.exp = exp;
197-
this.name = name;
198182
}
199183

200184
visit(visitor) {
@@ -459,7 +443,6 @@ export class AstVisitor {
459443
visitBinary(ast:Binary) {}
460444
visitChain(ast:Chain){}
461445
visitConditional(ast:Conditional) {}
462-
visitFormatter(ast:Formatter) {}
463446
visitPipe(ast:Pipe) {}
464447
visitFunctionCall(ast:FunctionCall) {}
465448
visitImplicitReceiver(ast:ImplicitReceiver) {}

modules/angular2/src/change_detection/parser/parser.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import {
1313
Binary,
1414
PrefixNot,
1515
Conditional,
16-
Formatter,
1716
Pipe,
1817
Assignment,
1918
Chain,
@@ -57,7 +56,7 @@ export class Parser {
5756
if (ListWrapper.isEmpty(pipes)) return bindingAst;
5857

5958
var res = ListWrapper.reduce(pipes,
60-
(result, currentPipeName) => new Pipe(result, currentPipeName),
59+
(result, currentPipeName) => new Pipe(result, currentPipeName, []),
6160
bindingAst.ast);
6261
return new ASTWithSource(res, bindingAst.source, bindingAst.location);
6362
}
@@ -191,7 +190,7 @@ class _ParseAST {
191190
parseChain():AST {
192191
var exprs = [];
193192
while (this.index < this.tokens.length) {
194-
var expr = this.parseFormatter();
193+
var expr = this.parsePipe();
195194
ListWrapper.push(exprs, expr);
196195

197196
if (this.optionalCharacter($SEMICOLON)) {
@@ -208,18 +207,18 @@ class _ParseAST {
208207
return new Chain(exprs);
209208
}
210209

211-
parseFormatter() {
210+
parsePipe() {
212211
var result = this.parseExpression();
213212
while (this.optionalOperator("|")) {
214213
if (this.parseAction) {
215-
this.error("Cannot have a formatter in an action expression");
214+
this.error("Cannot have a pipe in an action expression");
216215
}
217216
var name = this.expectIdentifierOrKeyword();
218217
var args = ListWrapper.create();
219218
while (this.optionalCharacter($COLON)) {
220219
ListWrapper.push(args, this.parseExpression());
221220
}
222-
result = new Formatter(result, name, args);
221+
result = new Pipe(result, name, args);
223222
}
224223
return result;
225224
}
@@ -380,7 +379,7 @@ class _ParseAST {
380379

381380
parsePrimary() {
382381
if (this.optionalCharacter($LPAREN)) {
383-
var result = this.parseFormatter();
382+
var result = this.parsePipe();
384383
this.expectCharacter($RPAREN);
385384
return result;
386385

modules/angular2/src/change_detection/proto_change_detector.js

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import {
1010
Binary,
1111
Chain,
1212
Conditional,
13-
Formatter,
1413
Pipe,
1514
FunctionCall,
1615
ImplicitReceiver,
@@ -40,14 +39,13 @@ import {
4039
RECORD_TYPE_INVOKE_CLOSURE,
4140
RECORD_TYPE_PRIMITIVE_OP,
4241
RECORD_TYPE_KEYED_ACCESS,
43-
RECORD_TYPE_INVOKE_FORMATTER,
4442
RECORD_TYPE_PIPE,
4543
RECORD_TYPE_INTERPOLATE
4644
} from './proto_record';
4745

4846
export class ProtoChangeDetector {
4947
addAst(ast:AST, bindingMemento:any, directiveMemento:any = null){}
50-
instantiate(dispatcher:any, formatters:Map):ChangeDetector{
48+
instantiate(dispatcher:any):ChangeDetector{
5149
return null;
5250
}
5351
}
@@ -68,10 +66,9 @@ export class DynamicProtoChangeDetector extends ProtoChangeDetector {
6866
this._recordBuilder.addAst(ast, bindingMemento, directiveMemento);
6967
}
7068

71-
instantiate(dispatcher:any, formatters:Map) {
69+
instantiate(dispatcher:any) {
7270
this._createRecordsIfNecessary();
73-
return new DynamicChangeDetector(dispatcher, formatters,
74-
this._pipeRegistry, this._records);
71+
return new DynamicChangeDetector(dispatcher, this._pipeRegistry, this._records);
7572
}
7673

7774
_createRecordsIfNecessary() {
@@ -99,9 +96,9 @@ export class JitProtoChangeDetector extends ProtoChangeDetector {
9996
this._recordBuilder.addAst(ast, bindingMemento, directiveMemento);
10097
}
10198

102-
instantiate(dispatcher:any, formatters:Map) {
99+
instantiate(dispatcher:any) {
103100
this._createFactoryIfNecessary();
104-
return this._factory(dispatcher, formatters, this._pipeRegistry);
101+
return this._factory(dispatcher, this._pipeRegistry);
105102
}
106103

107104
_createFactoryIfNecessary() {
@@ -178,10 +175,6 @@ class _ConvertAstIntoProtoRecords {
178175
return this._addRecord(RECORD_TYPE_PROPERTY, ast.name, ast.getter, [], null, receiver);
179176
}
180177

181-
visitFormatter(ast:Formatter) {
182-
return this._addRecord(RECORD_TYPE_INVOKE_FORMATTER, ast.name, ast.name, this._visitAll(ast.allArgs), null, 0);
183-
}
184-
185178
visitMethodCall(ast:MethodCall) {
186179
var receiver = ast.receiver.visit(this);
187180
var args = this._visitAll(ast.args);

modules/angular2/src/change_detection/proto_record.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export const RECORD_TYPE_PROPERTY = 3;
77
export const RECORD_TYPE_INVOKE_METHOD = 4;
88
export const RECORD_TYPE_INVOKE_CLOSURE = 5;
99
export const RECORD_TYPE_KEYED_ACCESS = 6;
10-
export const RECORD_TYPE_INVOKE_FORMATTER = 7;
1110
export const RECORD_TYPE_PIPE = 8;
1211
export const RECORD_TYPE_INTERPOLATE = 9;
1312

@@ -54,7 +53,6 @@ export class ProtoRecord {
5453

5554
isPureFunction():boolean {
5655
return this.mode === RECORD_TYPE_INTERPOLATE ||
57-
this.mode === RECORD_TYPE_INVOKE_FORMATTER ||
5856
this.mode === RECORD_TYPE_PRIMITIVE_OP;
5957
}
6058
}

modules/angular2/src/core/compiler/view.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import {EventManager} from 'angular2/src/core/events/event_manager';
1919

2020
const NG_BINDING_CLASS = 'ng-binding';
2121
const NG_BINDING_CLASS_SELECTOR = '.ng-binding';
22-
// TODO(tbosch): Cannot use `const` because of Dart.
23-
var NO_FORMATTERS = MapWrapper.create();
2422

2523
// TODO(rado): make this configurable/smarter.
2624
var VIEW_POOL_CAPACITY = 10000;
@@ -50,7 +48,7 @@ export class View {
5048
constructor(proto:ProtoView, nodes:List<Node>, protoChangeDetector:ProtoChangeDetector, protoContextLocals:Map) {
5149
this.proto = proto;
5250
this.nodes = nodes;
53-
this.changeDetector = protoChangeDetector.instantiate(this, NO_FORMATTERS);
51+
this.changeDetector = protoChangeDetector.instantiate(this);
5452
this.elementInjectors = null;
5553
this.rootElementInjectors = null;
5654
this.textNodes = null;

0 commit comments

Comments
 (0)