ZoolWay
ZoolWay

Reputation: 5505

Get ViewModel of containerless element

In Aurelia when I want to access the view model of a DOM element which is an aurelia custom element I can use the au property which Aurelia attaches, like componentElement.au.controller.viewModel.

When my custom element is containerless (attribute @containerless on class level) the property au is not available.

This gist demonstrates this: https://gist.run/?id=928f97f49c01c1db10d8bf4399f5c335

How can I access the viewmodel of a containerless custom component when I only have a reference to its DOM element?

Upvotes: 0

Views: 445

Answers (2)

Fabio
Fabio

Reputation: 11990

I'm not sure if this is what you want, but you can use view-model.ref. For instance:

<less-comp text="item three, containerless" view-model.ref="test"></less-comp>

Usage:

export class App {
  attached() {
    console.log(this.test);
  }
}

Upvotes: 1

bigopon
bigopon

Reputation: 1964

Achieve what you want by hooking into created life cycle in your view model:

class ViewModel {
  created(owningView, view) {
    view.controller
    view.controller.viewModel // <-- this is what you want
  }
}

Upvotes: 0

Related Questions