From 96d46c5ea81265e75e2972c4d6155f113022f2b5 Mon Sep 17 00:00:00 2001 From: James deBoer Date: Thu, 6 Mar 2014 12:23:37 -0800 Subject: [PATCH] chore(compiler): Move ViewFactory into WalkingViewFactory --- lib/core_dom/compiler.dart | 18 +++++++++++------- lib/core_dom/module.dart | 2 +- lib/core_dom/view_factory.dart | 21 ++++++++++++++------- test/core_dom/view_spec.dart | 9 +++++---- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/lib/core_dom/compiler.dart b/lib/core_dom/compiler.dart index 62c49a8b6..04fc34f3c 100644 --- a/lib/core_dom/compiler.dart +++ b/lib/core_dom/compiler.dart @@ -1,11 +1,15 @@ part of angular.core.dom; +abstract class Compiler implements Function { + WalkingViewFactory call(List elements, DirectiveMap directives); +} + @NgInjectableService() -class Compiler implements Function { +class WalkingCompiler implements Compiler { final Profiler _perf; final Expando _expando; - Compiler(this._perf, this._expando); + WalkingCompiler(this._perf, this._expando); List _compileView(NodeCursor domCursor, NodeCursor templateCursor, ElementBinder existingElementBinder, @@ -22,7 +26,7 @@ class Compiler implements Function { : existingElementBinder; if (elementBinder.hasTemplate) { - elementBinder.templateViewFactory = compileTransclusion( + elementBinder.templateViewFactory = _compileTransclusion( domCursor, templateCursor, elementBinder.template, elementBinder.templateBinder, directives); } @@ -50,7 +54,7 @@ class Compiler implements Function { return elementBinders; } - ViewFactory compileTransclusion( + WalkingViewFactory _compileTransclusion( NodeCursor domCursor, NodeCursor templateCursor, DirectiveRef directiveRef, ElementBinder transcludedElementBinder, @@ -66,7 +70,7 @@ class Compiler implements Function { _compileView(domCursor, transcludeCursor, transcludedElementBinder, directives); if (elementBinders == null) elementBinders = []; - viewFactory = new ViewFactory(transcludeCursor.elements, elementBinders, _perf, _expando); + viewFactory = new WalkingViewFactory(transcludeCursor.elements, elementBinders, _perf, _expando); domCursor.index = domCursorIndex; if (domCursor.isInstance) { @@ -84,7 +88,7 @@ class Compiler implements Function { return viewFactory; } - ViewFactory call(List elements, DirectiveMap directives) { + WalkingViewFactory call(List elements, DirectiveMap directives) { var timerId; assert((timerId = _perf.startTimer('ng.compile', _html(elements))) != false); final List domElements = elements; @@ -93,7 +97,7 @@ class Compiler implements Function { new NodeCursor(domElements), new NodeCursor(templateElements), null, directives); - var viewFactory = new ViewFactory(templateElements, + var viewFactory = new WalkingViewFactory(templateElements, elementBinders == null ? [] : elementBinders, _perf, _expando); assert(_perf.stopTimer(timerId) != false); diff --git a/lib/core_dom/module.dart b/lib/core_dom/module.dart index 6d6d7a065..8d52a5a20 100644 --- a/lib/core_dom/module.dart +++ b/lib/core_dom/module.dart @@ -39,7 +39,7 @@ class NgCoreDomModule extends Module { type(NgTextMustacheDirective); type(NgAttrMustacheDirective); - type(Compiler); + type(Compiler, implementedBy: WalkingCompiler); type(Http); type(UrlRewriter); type(HttpBackend); diff --git a/lib/core_dom/view_factory.dart b/lib/core_dom/view_factory.dart index 060d7c0ef..dab904f21 100644 --- a/lib/core_dom/view_factory.dart +++ b/lib/core_dom/view_factory.dart @@ -19,17 +19,23 @@ class BoundViewFactory { viewFactory(injector.createChild([new Module()..value(Scope, scope)])); } +abstract class ViewFactory implements Function { + BoundViewFactory bind(Injector injector); + + View call(Injector injector, [List elements]); +} + /** * ViewFactory is used to create new [View]s. ViewFactory is created by the * [Compiler] as a result of compiling a template. */ -class ViewFactory implements Function { +class WalkingViewFactory implements ViewFactory { final List elementBinders; final List templateElements; final Profiler _perf; final Expando _expando; - ViewFactory(this.templateElements, this.elementBinders, this._perf, this._expando) { + WalkingViewFactory(this.templateElements, this.elementBinders, this._perf, this._expando) { assert(elementBinders.forEach((ElementBinderTreeRef eb) { assert(eb is ElementBinderTreeRef); }) != true); } @@ -62,6 +68,7 @@ class ViewFactory implements Function { //List childElementBinders = eb.childElementBinders; int nodeListIndex = index + preRenderedIndexOffset; dom.Node node = nodeList[nodeListIndex]; + var binder = tree.binder; var timerId; try { @@ -75,16 +82,16 @@ class ViewFactory implements Function { parentNode.append(node); } - var childInjector = tree.binder != null ? tree.binder.bind(view, parentInjector, node) : parentInjector; - - if (tree.subtrees != null) { - _link(view, node.nodes, tree.subtrees, childInjector); - } + var childInjector = binder != null ? binder.bind(view, parentInjector, node) : parentInjector; if (fakeParent) { // extract the node from the parentNode. nodeList[nodeListIndex] = parentNode.nodes[0]; } + + if (tree.subtrees != null) { + _link(view, node.nodes, tree.subtrees, childInjector); + } } finally { assert(_perf.stopTimer(timerId) != false); } diff --git a/test/core_dom/view_spec.dart b/test/core_dom/view_spec.dart index b7cd6cf90..cecaa4e29 100644 --- a/test/core_dom/view_spec.dart +++ b/test/core_dom/view_spec.dart @@ -60,6 +60,7 @@ class BFilter { main() { + var viewFactoryFactory = (a,b,c,d) => new WalkingViewFactory(a,b,c,d); describe('View', () { var anchor; var $rootElement; @@ -77,8 +78,8 @@ main() { $rootElement.html(''); anchor = new ViewPort($rootElement.contents().eq(0)[0], injector.get(NgAnimate)); - a = (new ViewFactory($('Aa'), [], perf, expando))(injector); - b = (new ViewFactory($('Bb'), [], perf, expando))(injector); + a = (viewFactoryFactory($('Aa'), [], perf, expando))(injector); + b = (viewFactoryFactory($('Bb'), [], perf, expando))(injector); })); @@ -141,10 +142,10 @@ main() { LoggerViewDirective, new NgDirective(children: NgAnnotation.TRANSCLUDE_CHILDREN, selector: 'foo'), ''); - directiveRef.viewFactory = new ViewFactory($('text'), [], perf, new Expando()); + directiveRef.viewFactory = viewFactoryFactory($('text'), [], perf, new Expando()); var binder = ebf.binder(); binder.setTemplateInfo(0, [ directiveRef ]); - var outerViewType = new ViewFactory( + var outerViewType = viewFactoryFactory( $(''), [binder], perf,