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

Use scope.watchAST instead of scope.watch #1127

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/core_dom/element_binder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class TemplateElementBinder extends ElementBinder {

TemplateElementBinder(perf, expando, parser, config,
this.template, this.templateBinder,
onEvents, bindAttrs, childMode)
onEvents, bindAttrs, childMode, helperAst)
: super(perf, expando, parser, config,
null, null, onEvents, bindAttrs, childMode);
null, null, onEvents, bindAttrs, childMode, helperAst);

String toString() => "[TemplateElementBinder template:$template]";

Expand All @@ -44,6 +44,7 @@ class ElementBinder {
final Expando _expando;
final Parser _parser;
final CompilerConfig _config;
AST _helperAst;

final Map onEvents;
final Map bindAttrs;
Expand All @@ -58,7 +59,7 @@ class ElementBinder {

ElementBinder(this._perf, this._expando, this._parser, this._config,
this.componentData, this.decorators,
this.onEvents, this.bindAttrs, this.childMode);
this.onEvents, this.bindAttrs, this.childMode, this._helperAst);

final bool hasTemplate = false;

Expand Down
8 changes: 4 additions & 4 deletions lib/core_dom/element_binder_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ part of angular.core.dom_internal;
@Injectable()
class ElementBinderFactory {
final Parser _parser;
final ASTParser _astParser;
final Profiler _perf;
final CompilerConfig _config;
final Expando _expando;
Expand All @@ -11,21 +12,20 @@ class ElementBinderFactory {
final ShadowDomComponentFactory shadowDomComponentFactory;
final TranscludingComponentFactory transcludingComponentFactory;

ElementBinderFactory(this._parser, this._perf, this._config, this._expando,
ElementBinderFactory(this._parser, this._astParser, this._perf, this._config, this._expando,
this.astParser, this.componentFactory, this.shadowDomComponentFactory, this.transcludingComponentFactory);

// TODO: Optimize this to re-use a builder.
ElementBinderBuilder builder(FormatterMap formatters, DirectiveMap directives) =>
new ElementBinderBuilder(this,formatters, directives);

ElementBinder binder(ElementBinderBuilder b) =>

new ElementBinder(_perf, _expando, _parser, _config,
b.componentData, b.decorators, b.onEvents, b.bindAttrs, b.childMode);
b.componentData, b.decorators, b.onEvents, b.bindAttrs, b.childMode, _astParser('1'));

TemplateElementBinder templateBinder(ElementBinderBuilder b, ElementBinder transclude) =>
new TemplateElementBinder(_perf, _expando, _parser, _config,
b.template, transclude, b.onEvents, b.bindAttrs, b.childMode);
b.template, transclude, b.onEvents, b.bindAttrs, b.childMode, _astParser('1'));
}

/**
Expand Down
1 change: 1 addition & 0 deletions lib/directive/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import 'package:angular/core/module_internal.dart';
import 'package:angular/core/parser/parser.dart';
import 'package:angular/core_dom/module_internal.dart';
import 'package:angular/utils.dart';
import 'package:angular/change_detection/ast_parser.dart';
import 'package:angular/change_detection/watch_group.dart';
import 'package:angular/change_detection/change_detection.dart';
import 'package:angular/directive/static_keys.dart';
Expand Down
19 changes: 12 additions & 7 deletions lib/directive/ng_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class _NoopModelConverter extends NgModelConverter {
@Decorator(selector: '[ng-model]')
class NgModel extends NgControl implements AttachAware {
final Scope _scope;
final ASTParser _astParser;
AST _ast;
AST _collectionAst;

BoundSetter setter = (_, [__]) => null;

Expand All @@ -44,11 +47,14 @@ class NgModel extends NgControl implements AttachAware {
Watch _watch;
bool _watchCollection;

NgModel(this._scope, NgElement element, Injector injector, NodeAttrs attrs,
NgModel(this._scope, NgElement element, Injector injector, NodeAttrs attrs, this._astParser,
Animate animate)
: super(element, injector, animate)
{
_expression = attrs["ng-model"];
_ast = _astParser(_expression);
_collectionAst = _astParser(_expression, collection: true);

watchCollection = false;

//Since the user will never be editing the value of a select element then
Expand Down Expand Up @@ -132,7 +138,7 @@ class NgModel extends NgControl implements AttachAware {

// TODO(misko): could we get rid of watch collection, and just always watch the collection?
bool get watchCollection => _watchCollection;
void set watchCollection(value) {
void set watchCollection(bool value) {
if (_watchCollection == value) return;

var onChange = (value, [_]) {
Expand All @@ -145,14 +151,13 @@ class NgModel extends NgControl implements AttachAware {
_watchCollection = value;
if (_watch!=null) _watch.remove();
if (_watchCollection) {
_watch = _scope.watch(_expression, (changeRecord, _) {
_watch = _scope.watchAST(_collectionAst, (changeRecord, _) {
onChange(changeRecord is CollectionChangeRecord
? changeRecord.iterable
: changeRecord);
},
collection: true);
} else if (_expression != null) {
_watch = _scope.watch(_expression, onChange);
});
} else if (_ast != null) {
_watch = _scope.watchAST(_ast, onChange);
}
}

Expand Down
11 changes: 6 additions & 5 deletions test/directive/ng_model_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library ng_model_spec;

import '../_specs.dart';
import 'dart:html' as dom;
import 'package:angular/change_detection/ast_parser.dart';

//-----------------------------------------------------------------------------
// Utility functions
Expand Down Expand Up @@ -95,7 +96,7 @@ void main() {

nodeAttrs['ng-model'] = 'model';
var model = new NgModel(scope, ngElement, i.createChild([new Module()]),
nodeAttrs, new Animate());
nodeAttrs, i.get(ASTParser), new Animate());
dom.querySelector('body').append(element);
var input = new InputTextLike(element, model, scope, ngModelOptions);

Expand Down Expand Up @@ -375,7 +376,7 @@ void main() {

nodeAttrs['ng-model'] = 'model';
var model = new NgModel(scope, ngElement, i.createChild([new Module()]),
nodeAttrs, new Animate());
nodeAttrs, i.get(ASTParser), new Animate());
dom.querySelector('body').append(element);
var input = new InputTextLike(element, model, scope, ngModelOptions);

Expand Down Expand Up @@ -467,7 +468,7 @@ void main() {

nodeAttrs['ng-model'] = 'model';
var model = new NgModel(scope, ngElement, i.createChild([new Module()]),
nodeAttrs, new Animate());
nodeAttrs, i.get(ASTParser), new Animate());
dom.querySelector('body').append(element);
var input = new InputTextLike(element, model, scope, ngModelOptions);

Expand Down Expand Up @@ -567,7 +568,7 @@ void main() {

nodeAttrs['ng-model'] = 'model';
var model = new NgModel(scope, ngElement, i.createChild([new Module()]),
nodeAttrs, new Animate());
nodeAttrs, i.get(ASTParser), new Animate());
dom.querySelector('body').append(element);
var input = new InputTextLike(element, model, scope, ngModelOptions);

Expand Down Expand Up @@ -778,7 +779,7 @@ void main() {

nodeAttrs['ng-model'] = 'model';
var model = new NgModel(scope, ngElement, i.createChild([new Module()]),
nodeAttrs, new Animate());
nodeAttrs, i.get(ASTParser), new Animate());
dom.querySelector('body').append(element);
var input = new InputTextLike(element, model, scope, ngModelOptions);

Expand Down