From 9ad2a686860b21e555587dd2986ca77b969919cc Mon Sep 17 00:00:00 2001 From: James deBoer Date: Thu, 27 Feb 2014 12:43:10 -0800 Subject: [PATCH] fix(introspection): Better error messages and checked mode support --- lib/introspection.dart | 17 +++++++++++------ test/introspection_spec.dart | 11 +++++++++-- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/introspection.dart b/lib/introspection.dart index ca9b04cd0..e2312b2e6 100644 --- a/lib/introspection.dart +++ b/lib/introspection.dart @@ -15,11 +15,16 @@ var _elementExpando = new Expando('element'); * function is not intended to be called from Angular application. */ ElementProbe ngProbe(dom.Node node) { + if (node == null) { + throw "ngProbe called without node"; + } + var origNode = node; while(node != null) { var probe = _elementExpando[node]; if (probe != null) return probe; node = node.parent; } + throw "Could not find a probe for [$origNode]"; return null; } @@ -76,12 +81,12 @@ List ngDirectives(dom.Node node) { } _publishToJavaScript() { - // Point of style here: cascades require too many ()s, reducing readability. - js.context['ngProbe'] = (dom.Node node) => _jsProbe(ngProbe(node)); - js.context['ngInjector'] = (dom.Node node) => _jsInjector(ngInjector(node)); - js.context['ngScope'] = (dom.Node node) => _jsScope(ngScope(node)); - js.context['ngQuery'] = (dom.Node node, String selector, [String containsText]) => - new js.JsArray.from(ngQuery(node, selector, containsText)); + js.context + ..['ngProbe'] = new js.JsFunction.withThis((_, dom.Node node) => _jsProbe(ngProbe(node))) + ..['ngInjector'] = new js.JsFunction.withThis((_, dom.Node node) => _jsInjector(ngInjector(node))) + ..['ngScope'] = new js.JsFunction.withThis((_, dom.Node node) => _jsScope(ngScope(node))) + ..['ngQuery'] = new js.JsFunction.withThis((_, dom.Node node, String selector, [String containsText]) => + new js.JsArray.from(ngQuery(node, selector, containsText))); } js.JsObject _jsProbe(ElementProbe probe) { diff --git a/test/introspection_spec.dart b/test/introspection_spec.dart index f95c7b96c..d30c9b139 100644 --- a/test/introspection_spec.dart +++ b/test/introspection_spec.dart @@ -38,12 +38,19 @@ void main() { }); // Does not work in dart2js. deboer is investigating. - xit('should be available from Javascript', () { - ngBootstrap(element: new Element.html('
')); + it('should be available from Javascript', () { + // The probe only works if there is a directive. + var elt = $('
')[0]; + // Make it possible to find the element from JS + document.body.append(elt); + ngBootstrap(element: elt); + expect(js.context['ngProbe']).toBeDefined(); expect(js.context['ngScope']).toBeDefined(); expect(js.context['ngInjector']).toBeDefined(); expect(js.context['ngQuery']).toBeDefined(); + + expect(js.context['ngProbe'].apply([js.context['ngtop']])).toBeDefined(); }); }); }