简体   繁体   中英

Access model in view model in Aurelia Compose

So according to the heading Dynamically Render UI into the DOM based on Data here (scroll down a bit).

<template repeat.for="item of items">
    <compose model.bind="item" view-model="widgets/${item.type}"></compose>
</template>

If you take it more simply:

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

If I have an itemViewModel.html and a itemViewModel.js , they both get successfully loaded. However, how do you access the bound model in the itemViewModel.js ?

I tried using the bindable .

import {bindable, bindingMode} from 'aurelia-framework';

export class ItemViewModel {
    @bindable model;

    constructor() {
        console.log("using bindable", this);
    }
}

Is this possible?

<compose model.bind="item" will call a method activate(model) in your viewmodel and give you the item .

// the model received here is the *item* from the above <compose
activate(model){
   this.model = model;
}

If you want to pass multiple models in the bind you can do

<compose model.bind="{item: item, value: value}"

Then you get:

activate(model){
    this.item = model.item;
    this.value = model.value;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM