Skip to content
This repository was archived by the owner on Feb 22, 2018. It is now read-only.

Commit e4dfb46

Browse files
committed
feat(scope): Allow expressions on non-scope context
1 parent 195f043 commit e4dfb46

File tree

4 files changed

+37
-20
lines changed

4 files changed

+37
-20
lines changed

lib/core/scope.dart

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,19 @@ class Scope {
127127
Scope(Object this.context, this.rootScope, this._parentScope, this._depth,
128128
this._index, this.watchGroup, this.observeGroup);
129129

130-
Watch watch(expression, ReactionFn reactionFn) {
131-
// Todo(misko): remove the parser from here. It should only take AST.
130+
Watch watch(expression, ReactionFn reactionFn, {context, FilterMap filters}) {
132131
assert(expression != null);
133-
AST ast = expression is AST ? expression : rootScope._parse(expression);
132+
AST ast = expression is AST
133+
? expression
134+
: rootScope._astParser(expression, context: context, filters: filters);
134135
return watchGroup.watch(ast, reactionFn);
135136
}
136137

137-
Watch observe(expression, ReactionFn reactionFn) {
138-
// Todo(misko): remove the parser from here. It should only take AST.
138+
Watch observe(expression, ReactionFn reactionFn, {context, FilterMap filters}) {
139139
assert(expression != null);
140-
AST ast = expression is AST ? expression : rootScope._parse(expression);
140+
AST ast = expression is AST
141+
? expression
142+
: rootScope._astParser(expression, context: context, filters: filters);
141143
return observeGroup.watch(ast, reactionFn);
142144
}
143145

@@ -218,6 +220,7 @@ class RootScope extends Scope {
218220
static final STATE_FLUSH = 'digest';
219221

220222
final ExceptionHandler _exceptionHandler;
223+
final AstParser _astParser;
221224
final Parser _parser;
222225
final ScopeDigestTTL _ttl;
223226
final ExpressionVisitor visitor = new ExpressionVisitor(); // TODO(misko): delete me
@@ -229,8 +232,9 @@ class RootScope extends Scope {
229232

230233
String _state;
231234

232-
RootScope(Object context, this._parser, GetterCache cacheGetter,
233-
FilterMap filterMap, this._exceptionHandler, this._ttl, this._zone)
235+
RootScope(Object context, this._astParser, this._parser,
236+
GetterCache cacheGetter, FilterMap filterMap,
237+
this._exceptionHandler, this._ttl, this._zone)
234238
: super(context, null, null, 0, 0,
235239
new RootWatchGroup(new DirtyCheckingChangeDetector(cacheGetter), context),
236240
new RootWatchGroup(new DirtyCheckingChangeDetector(cacheGetter), context))
@@ -357,7 +361,6 @@ class RootScope extends Scope {
357361
}
358362

359363

360-
AST _parse(expression) => visitor.visit(_parser.call(expression));
361364
void destroy() {}
362365

363366
void _transitionState(String from, String to) {

lib/core_dom/compiler.dart

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ part of angular.core.dom;
44
class Compiler {
55
final Profiler _perf;
66
final Parser _parser;
7-
final AstParser _astParser;
87
final Expando _expando;
98

10-
Compiler(this._perf, this._parser, this._astParser, this._expando);
9+
Compiler(this._perf, this._parser, this._expando);
1110

1211
_compileBlock(NodeCursor domCursor, NodeCursor templateCursor,
1312
List<DirectiveRef> useExistingDirectiveRefs,
@@ -160,25 +159,28 @@ class Compiler {
160159
var blockOutbound = false;
161160
var blockInbound = false;
162161
scope.watch(
163-
_astParser(expression, filters: filters),
162+
expression,
164163
(inboundValue, _) {
165164
if (!blockInbound) {
166165
blockOutbound = true;
167166
scope.rootScope.runAsync(() => blockOutbound = false);
168167
return dstPathFn.assign(controller, inboundValue);
169168
}
170-
}
169+
},
170+
filters: filters
171171
);
172172
if (expressionFn.isAssignable) {
173173
scope.watch(
174-
_astParser(dstExpression, context: controller, filters: filters),
174+
dstExpression,
175175
(outboundValue, _) {
176176
if(!blockOutbound) {
177177
blockInbound = true;
178178
scope.rootScope.runAsync(() => blockInbound = false);
179179
expressionFn.assign(scope.context, outboundValue);
180180
}
181-
}
181+
},
182+
context: controller,
183+
filters: filters
182184
);
183185
}
184186
};
@@ -188,8 +190,9 @@ class Compiler {
188190
if (attrs[attrName] == null) return;
189191
Expression attrExprFn = _parser(attrs[attrName]);
190192
var shadowValue = null;
191-
scope.watch(_astParser(attrs[attrName], filters: filters),
192-
(v, _) => dstPathFn.assign(controller, shadowValue = v));
193+
scope.watch(attrs[attrName],
194+
(v, _) => dstPathFn.assign(controller, shadowValue = v),
195+
filters: filters);
193196
};
194197
break;
195198
case '=>!':
@@ -198,12 +201,13 @@ class Compiler {
198201
Expression attrExprFn = _parser(attrs[attrName]);
199202
var watch;
200203
watch = scope.watch(
201-
_astParser(attrs[attrName], filters: filters),
204+
attrs[attrName],
202205
(value, _) {
203206
if (dstPathFn.assign(controller, value) != null) {
204207
watch.remove();
205208
}
206-
});
209+
},
210+
filters: filters);
207211
};
208212
break;
209213
case '&':

lib/directive/input_select.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class InputSelectDirective implements NgAttachAware {
6363
_selectElement.onChange.listen((event) => _mode.onViewChange(event));
6464
_model.render = (value) {
6565
// TODO(misko): this hack need to delay the rendering until after domRead
66-
// becouse the modelChange reads from the DOM. We should be able to render
66+
// because the modelChange reads from the DOM. We should be able to render
6767
// without DOM changes.
6868
_scope.rootScope.domRead(() {
6969
_scope.rootScope.domWrite(() => _mode.onModelChange(value));

test/core/scope_spec.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,16 @@ main() => describe('scope', () {
755755
}));
756756

757757

758+
it('should watch/observe on objects other then contex', inject((RootScope rootScope) {
759+
var log = '';
760+
var map = {'a': 'A', 'b': 'B'};
761+
rootScope.watch('a', (a, b) => log += a, context: map);
762+
rootScope.watch('b', (a, b) => log += a, context: map);
763+
rootScope.apply();
764+
expect(log).toEqual('AB');
765+
}));
766+
767+
758768
it(r'should watch and fire on expression change', inject((RootScope rootScope) {
759769
var log;
760770

0 commit comments

Comments
 (0)