1

It is possible to pass attributes to Aurelia custom element with @bindable decorator:

export class ItemCustomElement {
    @bindable model: Item;
}

<item model.bind="model"></item>

Why custom elements rendered by <compose> are treated differently, according to docs they need an activate method to pass data? @bindable is not respected.

export class ItemCustomElement {
    @bindable model: Item;

    activate(model: Item): void {
        this.model = model;
    }
}

<compose view-model="./item" model.bind="model"></compose>

From the custom element's point of view, currently it needs to know how it is going to be used, with <compose> or not. I think a custom element should be isolated from this external decision. Can we make @bindable work in both cases?

zakjan
  • 2,391
  • 1
  • 19
  • 29

1 Answers1

0

Custom Elements rendered by <compose> have access to their outer scopes. So, there is no need to use @bindable. See this example https://gist.run/?id=fae6b9c9c2e3a608a60522392329bae1

Fabio
  • 11,892
  • 1
  • 25
  • 41
  • In your example, how do you access someMessage property inside MyElement class? – zakjan Jul 25 '16 at 12:55
  • I could access because it was "imported" using ``. When you use compose, the element can access parent's properties directly – Fabio Jul 25 '16 at 13:08
  • Yeah, in the view HTML template. I'm asking about the view-model JS file. – zakjan Jul 25 '16 at 13:08
  • `@bindable` is the preferred way to put a custom element attribute to a class property, so it is possible to access it with `this.someMessage`. I'd like to use it in the same way also if a custom element is rendered with ``. – zakjan Jul 25 '16 at 13:11
  • you can use `parentOverrideContext` from `bind()` method. See the updated example https://gist.run/?id=fae6b9c9c2e3a608a60522392329bae1 This is the only way I have found, I am not sure if there is not an easier way – Fabio Jul 25 '16 at 13:23
  • 1
    I'm sorry but I don't see it as a solution. I need to have a custom element JS code independant on the way how it is rendered, with `` or not. I believe `@bindable` should work with ``. – zakjan Jul 25 '16 at 20:47