Skip to content

Commit dcd905a

Browse files
committed
feat(change_detector): add support for formatters
1 parent 79a9430 commit dcd905a

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

modules/change_detection/src/record.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const PROTO_RECORD_PROPERTY = 'property';
1616
export class ProtoRecord {
1717
@FIELD('final watchGroup:wg.ProtoWatchGroup')
1818
@FIELD('final context:Object')
19+
@FIELD('final funcOrValue:Object')
1920
@FIELD('final arity:int')
2021
@FIELD('final dest')
2122

modules/change_detection/src/watch_group.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import {ProtoRecord, Record, PROTO_RECORD_CONST, PROTO_RECORD_FUNC, PROTO_RECORD_PROPERTY} from './record';
22
import {FIELD, IMPLEMENTS, isBlank, isPresent, int, toBool, autoConvertAdd, BaseException} from 'facade/lang';
33
import {ListWrapper} from 'facade/collection';
4-
import {AST, AccessMember, ImplicitReceiver, AstVisitor, LiteralPrimitive, Binary} from './parser/ast';
4+
import {AST, AccessMember, ImplicitReceiver, AstVisitor, LiteralPrimitive, Binary, Formatter} from './parser/ast';
55

66
export class ProtoWatchGroup {
77
@FIELD('headRecord:ProtoRecord')
88
@FIELD('tailRecord:ProtoRecord')
9-
constructor() {
9+
constructor(formatters) {
10+
this.formatters = formatters;
11+
1012
this.headRecord = null;
1113
this.tailRecord = null;
1214
}
@@ -179,6 +181,15 @@ class ProtoRecordCreator {
179181
this.add(record);
180182
}
181183

184+
visitFormatter(ast:Formatter, dest) {
185+
var formatter = this.protoWatchGroup.formatters[ast.name];
186+
var record = this.construct(PROTO_RECORD_FUNC, formatter, ast.allArgs.length, dest);
187+
for (var i = 0; i < ast.allArgs.length; ++i) {
188+
ast.allArgs[i].visit(this, new Destination(record, i));
189+
}
190+
this.add(record);
191+
}
192+
182193
createRecordsFromAST(ast:AST, memento){
183194
ast.visit(this, memento);
184195
}

modules/change_detection/test/change_detector_spec.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ export function main() {
2121
return parser.parseBinding(exp);
2222
}
2323

24-
function createChangeDetector(memo:string, exp:string, context = null) {
25-
var pwg = new ProtoWatchGroup();
24+
function createChangeDetector(memo:string, exp:string, context = null, formatters = null) {
25+
var pwg = new ProtoWatchGroup(formatters);
2626
pwg.watch(ast(exp), memo, false);
2727

2828
var dispatcher = new LoggingDispatcher();
@@ -34,8 +34,8 @@ export function main() {
3434
return {"changeDetector" : cd, "dispatcher" : dispatcher};
3535
}
3636

37-
function executeWatch(memo:string, exp:string, context = null) {
38-
var res = createChangeDetector(memo, exp, context);
37+
function executeWatch(memo:string, exp:string, context = null, formatters = null) {
38+
var res = createChangeDetector(memo, exp, context, formatters);
3939
res["changeDetector"].detectChanges();
4040
return res["dispatcher"].log;
4141
}
@@ -61,19 +61,19 @@ export function main() {
6161
expect(dispatcher.log).toEqual(['name=Misko']);
6262
});
6363

64-
it('should watch chained properties', () => {
64+
it('should support chained properties', () => {
6565
var address = new Address('Grenoble');
6666
var person = new Person('Victor', address);
6767

6868
expect(executeWatch('address.city', 'address.city', person))
6969
.toEqual(['address.city=Grenoble']);
7070
});
7171

72-
it("should watch literals", () => {
72+
it("should support literals", () => {
7373
expect(executeWatch('const', '10')).toEqual(['const=10']);
7474
});
7575

76-
it("should watch binary operations", () => {
76+
it("should support binary operations", () => {
7777
expect(executeWatch('exp', '10 + 2')).toEqual(['exp=12']);
7878
expect(executeWatch('exp', '10 - 2')).toEqual(['exp=8']);
7979

@@ -104,6 +104,15 @@ export function main() {
104104
expect(executeWatch('exp', 'true || false')).toEqual(['exp=true']);
105105
expect(executeWatch('exp', 'false || false')).toEqual(['exp=false']);
106106
});
107+
108+
it("should support formatters", () => {
109+
var formatters = {
110+
"uppercase" : (v) => v.toUpperCase(),
111+
"wrap" : (v, before, after) => `${before}${v}${after}`
112+
};
113+
expect(executeWatch('str', '"aBc" | uppercase', null, formatters)).toEqual(['str=ABC']);
114+
expect(executeWatch('str', '"b" | wrap:"a":"c"', null, formatters)).toEqual(['str=abc']);
115+
});
107116
});
108117
});
109118
}

modules/core/test/compiler/view_spec.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ export function main() {
100100
beforeEach(() => {
101101
var template = DOM.createTemplate(tempalteWithThreeTypesOfBindings);
102102
var pv = new ProtoView(template, templateElementBinders(),
103-
new ProtoWatchGroup(), false);
103+
new ProtoWatchGroup(null), false);
104104
view = pv.instantiate(null, null);
105105
});
106106

@@ -142,7 +142,12 @@ export function main() {
142142
describe('react to watch group changes', () => {
143143
var view, cd, ctx;
144144

145-
function createView(protoView) {
145+
var protoWatchGroup = new ProtoWatchGroup();
146+
protoWatchGroup.watch(oneFieldAst('foo'), memento);
147+
148+
var pv = new ProtoView(template, templateElementBinders(),
149+
protoWatchGroup, false);
150+
146151
ctx = new MyEvaluationContext();
147152
view = protoView.instantiate(ctx, null);
148153
cd = new ChangeDetector(view.watchGroup);

0 commit comments

Comments
 (0)