UNPKG

1.62 kBPlain TextView Raw
1import { customAttribute } from 'aurelia-templating';
2import { getLogger, Logger } from 'aurelia-logging';
3
4/**
5 * Attribute to be placed on any HTML element in a view to emit the View instance
6 * to the debug console, giving you insight into the live View instance, including
7 * all child views, live bindings, behaviors and more.
8 */
9@customAttribute('view-spy')
10export class ViewSpy {
11 private logger: Logger;
12 private value: any;
13 private view: any;
14
15 /**
16 * Creates a new instance of ViewSpy.
17 */
18 constructor() {
19 this.logger = getLogger('view-spy');
20 }
21
22 private _log(lifecycleName: string, context?: {}) {
23 if (!this.value && lifecycleName === 'created') {
24 this.logger.info(lifecycleName, this.view);
25 } else if (this.value && this.value.indexOf(lifecycleName) !== -1) {
26 this.logger.info(lifecycleName, this.view, context);
27 }
28 }
29
30 /**
31 * Invoked when the target view is created.
32 * @param view The target view.
33 */
34 public created(view: any) {
35 this.view = view;
36 this._log('created');
37 }
38
39 /**
40 * Invoked when the target view is bound.
41 * @param bindingContext The target view's binding context.
42 */
43 public bind(bindingContext: {}) {
44 this._log('bind', bindingContext);
45 }
46
47 /**
48 * Invoked when the target element is attached to the DOM.
49 */
50 public attached() {
51 this._log('attached');
52 }
53
54 /**
55 * Invoked when the target element is detached from the DOM.
56 */
57 public detached() {
58 this._log('detached');
59 }
60
61 /**
62 * Invoked when the target element is unbound.
63 */
64 public unbind() {
65 this._log('unbind');
66 }
67}