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

Commit

Permalink
chore(compiler): Move ViewFactory into WalkingViewFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
jbdeboer committed Mar 11, 2014
1 parent 811b460 commit 96d46c5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
18 changes: 11 additions & 7 deletions lib/core_dom/compiler.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
part of angular.core.dom;

abstract class Compiler implements Function {
WalkingViewFactory call(List<dom.Node> 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<ElementBinderTreeRef> _compileView(NodeCursor domCursor, NodeCursor templateCursor,
ElementBinder existingElementBinder,
Expand All @@ -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);
}
Expand Down Expand Up @@ -50,7 +54,7 @@ class Compiler implements Function {
return elementBinders;
}

ViewFactory compileTransclusion(
WalkingViewFactory _compileTransclusion(
NodeCursor domCursor, NodeCursor templateCursor,
DirectiveRef directiveRef,
ElementBinder transcludedElementBinder,
Expand All @@ -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) {
Expand All @@ -84,7 +88,7 @@ class Compiler implements Function {
return viewFactory;
}

ViewFactory call(List<dom.Node> elements, DirectiveMap directives) {
WalkingViewFactory call(List<dom.Node> elements, DirectiveMap directives) {
var timerId;
assert((timerId = _perf.startTimer('ng.compile', _html(elements))) != false);
final List<dom.Node> domElements = elements;
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/core_dom/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class NgCoreDomModule extends Module {
type(NgTextMustacheDirective);
type(NgAttrMustacheDirective);

type(Compiler);
type(Compiler, implementedBy: WalkingCompiler);
type(Http);
type(UrlRewriter);
type(HttpBackend);
Expand Down
21 changes: 14 additions & 7 deletions lib/core_dom/view_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<dom.Node> 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<ElementBinderTreeRef> elementBinders;
final List<dom.Node> 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);
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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);
}
Expand Down
9 changes: 5 additions & 4 deletions test/core_dom/view_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -77,8 +78,8 @@ main() {
$rootElement.html('<!-- anchor -->');
anchor = new ViewPort($rootElement.contents().eq(0)[0],
injector.get(NgAnimate));
a = (new ViewFactory($('<span>A</span>a'), [], perf, expando))(injector);
b = (new ViewFactory($('<span>B</span>b'), [], perf, expando))(injector);
a = (viewFactoryFactory($('<span>A</span>a'), [], perf, expando))(injector);
b = (viewFactoryFactory($('<span>B</span>b'), [], perf, expando))(injector);
}));


Expand Down Expand Up @@ -141,10 +142,10 @@ main() {
LoggerViewDirective,
new NgDirective(children: NgAnnotation.TRANSCLUDE_CHILDREN, selector: 'foo'),
'');
directiveRef.viewFactory = new ViewFactory($('<b>text</b>'), [], perf, new Expando());
directiveRef.viewFactory = viewFactoryFactory($('<b>text</b>'), [], perf, new Expando());
var binder = ebf.binder();
binder.setTemplateInfo(0, [ directiveRef ]);
var outerViewType = new ViewFactory(
var outerViewType = viewFactoryFactory(
$('<!--start--><!--end-->'),
[binder],
perf,
Expand Down

0 comments on commit 96d46c5

Please sign in to comment.