1
votes

I recently started trying out Aurelia (I'm new to Javascript and used to typed languages so I'm working from the typescript skeleton), and I ran into some odd issues with view inheritance that I don't understand.

I wanted to create two views with common functionality, which is why I wanted to create a common super class that both views could extend. This however caused unexpected behavior: when including both views in app.html, only one of the two shows, depending on the order of require statements.

I included a small minimal example below:

parent-view.ts

import {bindable} from 'aurelia-framework';   

export abstract class ParentView {
    @bindable title: string;
}

child-a-view.html

<template><div>${title} A</div></template>

child-a-view.ts

import {ParentView} from './parent-view'

export class ChildAView extends ParentView {
    constructor() {
        super()
    }
}

child-b-view.html

<template><div>${title} B</div></template>

child-b-view.ts

import {ParentView} from './parent-view'

export class ChildBView extends ParentView {
    constructor() {
        super()
    }
}

app.ts

export class App {}

app.html

<template>
    <require from="./child-a-view"></require>
    <child-a-view title="TitleA"></child-a-view>

    <require from="./child-b-view"></require>
    <child-b-view title="TitleB"></child-b-view>
</template>

This gives as output a page containing the text "TitleA B", while I'd expect two lines "TitleA A" and "TitleB B".

An alternative ordering of require statements in app.html

<template>
    <require from="./child-b-view"></require>
    <require from="./child-a-view"></require>
    <child-a-view title="TitleA"></child-a-view>
    <child-b-view title="TitleB"></child-b-view>
</template>

gives as output a page containing the text "TitleB A", while again I'd expect two lines "TitleA A" and "TitleB B".

Is this a bug in Aurelia, is it not allowed to inherit view classes, or am I doing something wrong here that causes this?

3

3 Answers

3
votes

It's not necessarily a bug in Aurelia, more a general problem with JavaScript. But yes, inheritance is known to be problematic with custom elements and it is not recommended.

Aurelia strongly favors the composition over inheritance.

If you need to share common code between two or more ViewModels, try to approach it the other way around: make one ViewModel with the "common" code, and then you make the stuff variable that you would otherwise put in the child ViewModels.

Also see this similar question

And as @Alec Buchanan already pointed out, you can easily combine two views into one custom element (thereby achieving the same effect as inheriting the ViewModel) by using compose.

For instance:

child-a-view.html

<template><div>${title} A</div></template>

child-b-view.html

<template><div>${title} B</div></template>

parent-view.ts

import {bindable} from 'aurelia-framework';   

export class ParentView {
    @bindable title: string;
}

parent-view.html

<template>
    <compose view="./child-a-view.html"></compose>
    <compose view="./child-b-view.html"></compose>
</template>

This will automatically set the binding context of the child views to that of the parent view, meaning you can tread the child view html files as if parent-view.ts was their ViewModel.

0
votes

Is it necessary to be trying to use these modules as custom elements? Could you not use the compose element to include each one?

eg

<template>
    <compose view-model="childAView" view="./child-a-view"></compose>
    <compose view-model="childBView" view="./child-b-view"></compose>
<template>
0
votes

The derived class need its own behavior. In your case, you need at least one decorator on the child class to tell Aurelia to inherit it right.