1

I need a way to access the list of objects that aurelia makes.

Specifically, I have file called current-user.ts. That file has this code in it:

export var currentUser = null; 

I need a way to access the value of this variable. Preferably without calling any functions. (I am trying to inject the user value into an app called Dynatrace.)

I have tried looking through the window and document.body.aurelia.container classes, but I can't find a way to grab the value.

Vaccano
  • 78,325
  • 149
  • 468
  • 850
  • If you want to access the value of the exported currentUser variable from current-user.ts, you should in the file where you want access: import currentUser from './current-user'; Can you explain further to determine better how to help? Regards. – Cristián Ormazábal Jun 07 '19 at 00:33
  • I am trying to access it from the Chrome Debug Console. Without changing my app. – Vaccano Jun 07 '19 at 14:31
  • 1
    Aurelia context is accesible from the Chrome Debug Console for the active viewmodel if you install the Aurelia Inspector Extension. See https://chrome.google.com/webstore/detail/aurelia-inspector/ofemgdknaajmpeoblfdjkenbpcfbdefg?hl=en. But the exported variable won't be accesible unless it is referenced by a viewmodel, so I don't think you'll be able to read its value without modifying your app. – Cristián Ormazábal Jun 07 '19 at 14:50

2 Answers2

4

At any element with Aurelia behavior (Custom Element/Custom Attribute), you can access the underlying view model via:

element.au.controller.viewModel

viewModel is often custom element, if you have custom element and custom attribute on the same element, and you want to access the custom attribute, you can do

element.au['my-custom-attribute']

to get the custom attribute view model.

Beside this, at the root element of your application, you can retrieve aurelia instance via

element.aurelia
bigopon
  • 1,924
  • 2
  • 14
  • 23
2

If you want to access the variable from both your Aurelia code and some other application/js code, using the window object is probably the easiest way to do it.

You could also add a getter/setter in the current-user class that updates both the internal property and the window object, e.g.

class currentUser {
    private _userName: string;
    get userName(): string {
        return this._userName;
    }
    set userName(userName: string) {
        this._userName = userName;
        window['currentUserName'] = userName;
    }
}

let user = new currentUser();
user.userName = 'foo';

// both will output 'foo'
console.log(user.userName);
console.log(window['currentUserName']);
Kjetil Ek
  • 111
  • 2
  • I am trying to access without changing my application. (I am trying to get the user info exposed to a Monitoring tool) – Vaccano Jun 07 '19 at 14:32