Skip to content

Commit

Permalink
feat(element binder): Use a child scope instead of Scope.watch(contex…
Browse files Browse the repository at this point in the history
…t:o)

This is the only place where Scope.watch(context:o) is used and it is
in the way of optimizations.

For dart-archive#1086
  • Loading branch information
jbdeboer committed May 30, 2014
1 parent 61f3348 commit 5c8d03e
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions lib/core_dom/element_binder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class ElementBinder {
bool get hasDirectivesOrEvents =>
_usableDirectiveRefs.isNotEmpty || onEvents.isNotEmpty;

void _bindTwoWay(tasks, expression, scope, dstPathFn, controller, formatters, dstExpression) {
void _bindTwoWay(tasks, expression, scope, controllerScope,
dstPathFn, controller, formatters, dstExpression) {
var taskId = tasks.registerTask();
Expression expressionFn = _parser(expression);

Expand All @@ -99,14 +100,14 @@ class ElementBinder {
}
}, formatters: formatters);
if (expressionFn.isAssignable) {
scope.watch(dstExpression, (outboundValue, _) {
controllerScope.watch(dstExpression, (outboundValue, _) {
if (!viewOutbound) {
viewInbound = true;
scope.rootScope.runAsync(() => viewInbound = false);
expressionFn.assign(scope.context, outboundValue);
tasks.completeTask(taskId);
}
}, context: controller, formatters: formatters);
}, formatters: formatters);
}
}

Expand All @@ -127,6 +128,7 @@ class ElementBinder {

void _createAttrMappings(directive, scope, List<MappingParts> mappings, nodeAttrs, formatters,
tasks) {
Scope controllerScope; // Only created if there is a two-way binding in the element.
mappings.forEach((MappingParts p) {
var attrName = p.attrName;
var dstExpression = p.dstExpression;
Expand All @@ -141,7 +143,10 @@ class ElementBinder {
var bindAttr = bindAttrs["bind-${p.attrName}"];
if (bindAttr != null) {
if (p.mode == '<=>') {
_bindTwoWay(tasks, bindAttr, scope, dstPathFn,
if (controllerScope == null) {
controllerScope = scope.createChild(directive);
}
_bindTwoWay(tasks, bindAttr, scope, controllerScope, dstPathFn,
directive, formatters, dstExpression);
} else if(p.mode == '&') {
_bindCallback(dstPathFn, directive, bindAttr, scope);
Expand All @@ -162,8 +167,10 @@ class ElementBinder {

case '<=>': // two-way
if (nodeAttrs[attrName] == null) return;

_bindTwoWay(tasks, nodeAttrs[attrName], scope, dstPathFn,
if (controllerScope == null) {
controllerScope = scope.createChild(directive);
}
_bindTwoWay(tasks, nodeAttrs[attrName], scope, controllerScope, dstPathFn,
directive, formatters, dstExpression);
break;

Expand Down

0 comments on commit 5c8d03e

Please sign in to comment.