This repository has been archived by the owner on Feb 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(probe): Access injector, scope, directives from REPL
Add global methods to make it easier to debug Angular application from the browser's REPL, unit or end-to-end tests. Closes #305
- Loading branch information
Showing
8 changed files
with
142 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
part of angular; | ||
|
||
/** | ||
* A global write only variable which keeps track of objects attached to the elements. | ||
* This is usefull for debugging AngularDart application from the browser's REPL. | ||
*/ | ||
Expando _elementExpando = new Expando('element'); | ||
|
||
/** | ||
* Return the closest [ElementProbe] object for a given [Element]. | ||
* | ||
* NOTE: This global method is here to make it easier to debug Angular application from | ||
* the browser's REPL, unit or end-to-end tests. The function is not intended to | ||
* be called from Angular application. | ||
*/ | ||
ElementProbe ngProbe(dom.Node node) { | ||
while(node != null) { | ||
var probe = _elementExpando[node]; | ||
if (probe != null) return probe; | ||
node = node.parent; | ||
} | ||
return null; | ||
} | ||
|
||
|
||
/** | ||
* Return the [Injector] associated with a current [Element]. | ||
* | ||
* **NOTE**: This global method is here to make it easier to debug Angular application from | ||
* the browser's REPL, unit or end-to-end tests. The function is not intended to be called | ||
* from Angular application. | ||
*/ | ||
Injector ngInjector(dom.Node node) => ngProbe(node).injector; | ||
|
||
|
||
/** | ||
* Return the [Scope] associated with a current [Element]. | ||
* | ||
* **NOTE**: This global method is here to make it easier to debug Angular application from | ||
* the browser's REPL, unit or end-to-end tests. The function is not intended to be called | ||
* from Angular application. | ||
*/ | ||
Scope ngScope(dom.Node node) => ngProbe(node).scope; | ||
|
||
|
||
/** | ||
* Return a List of directive controllers associated with a current [Element]. | ||
* | ||
* **NOTE**: This global method is here to make it easier to debug Angular application from | ||
* the browser's REPL, unit or end-to-end tests. The function is not intended to be called | ||
* from Angular application. | ||
*/ | ||
List<Object> ngDirectives(dom.Node node) { | ||
ElementProbe probe = _elementExpando[node]; | ||
return probe == null ? [] : probe.directives; | ||
} | ||
|
||
_publishToJavaScript() { | ||
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)); | ||
} | ||
|
||
JsObject _jsProbe(ElementProbe probe) { | ||
return new JsObject.jsify({ | ||
"element": probe.element, | ||
"injector": _jsInjector(probe.injector), | ||
"scope": _jsScope(probe.scope), | ||
"directives": probe.directives.map((directive) => _jsDirective(directive)) | ||
})..['_dart_'] = probe; | ||
} | ||
|
||
JsObject _jsInjector(Injector injector) { | ||
return new JsObject.jsify({ | ||
"get": injector.get | ||
})..['_dart_'] = injector; | ||
} | ||
|
||
JsObject _jsScope(Scope scope) { | ||
return new JsObject.jsify({ | ||
"\$apply": scope.$apply, | ||
"\$digest": scope.$digest, | ||
"get": (name) => scope[name], | ||
"set": (name, value) => scope[name] = value | ||
})..['_dart_'] = scope; | ||
} | ||
|
||
_jsDirective(directive) => directive; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
library introspection_spec; | ||
|
||
import '_specs.dart'; | ||
|
||
main() => describe('introspection', () { | ||
it('should retrieve ElementProbe', inject((TestBed _) { | ||
_.compile('<div ng-bind="true"></div>'); | ||
ElementProbe probe = ngProbe(_.rootElement); | ||
expect(probe.injector.parent).toBe(_.injector); | ||
expect(ngInjector(_.rootElement).parent).toBe(_.injector); | ||
expect(probe.directives[0] is NgBindDirective).toBe(true); | ||
expect(ngDirectives(_.rootElement)[0] is NgBindDirective).toBe(true); | ||
expect(probe.scope).toBe(_.rootScope); | ||
expect(ngScope(_.rootElement)).toBe(_.rootScope); | ||
})); | ||
}); |