Referencing standard Javascript global objects in a view

The standard Javascript global objects (Math, Object, JSON…) do not seem to be available in views, e.g. ${Math.PI} or ${Object.keys(someObject).length} yield nothing.

Someone of the core Aurelia team shared somewhere on Github (forgot exactly who or where, a reference would be appreciated for proper credit) a trick to reference TypeScript enums in view code, which works exactly the same for this case:

export class MyViewModel {
    Math = Math;
}

This makes the global object a property on the viewModel and thus makes it accessible to the view.

Nevertheless, are global objects unavailable in view code by design, just as a result of how the view/viewModel relationship is implemented, or simply because of a lack of useful use cases?

1 Like

Its more of a matter of the target Platform. Imagine we’d make window available, now what would happen in NodeJs? Sure there is global but its something completely different. By you defining props you have the control and thus can define what should happen

1 Like

FYI, there was an issue on this topic. https://github.com/aurelia/aurelia/issues/664

1 Like

For the reference, the mentioned view engine hook would be implemented like depicted here http://blog.williamhayes.org/2017/03/aurelia-view-engine-hook-for-importing.html?m=1

1 Like

Another great gem from @davismj

4 Likes