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

Commit

Permalink
fix(introspection): Better error messages and checked mode support
Browse files Browse the repository at this point in the history
  • Loading branch information
jbdeboer committed Feb 27, 2014
1 parent 5a36e77 commit 9ad2a68
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
17 changes: 11 additions & 6 deletions lib/introspection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -76,12 +81,12 @@ List<Object> 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) {
Expand Down
11 changes: 9 additions & 2 deletions test/introspection_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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('<div></div>'));
it('should be available from Javascript', () {
// The probe only works if there is a directive.
var elt = $('<div ng-app id=ngtop ng-bind="\'introspection FTW\'"></div>')[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();
});
});
}

0 comments on commit 9ad2a68

Please sign in to comment.