简体   繁体   English

Aurelia - 哪个视图端口是呈现的视图模型?

[英]Aurelia - which view port is the view-model being rendered in?

I have a two-column layout setup, the two columns have very similar functionality, so the very same view-model is being reused. 我有一个双列布局设置,两列具有非常相似的功能,因此正在重用相同的视图模型。 Hovewer, the rendering might slightly differ depending on which side is it being rendered, so wondering how is it possible to access view port information? Hovewer,渲染可能会略有不同,具体取决于渲染的哪一侧,所以想知道如何访问视口信息?

Consider this setup: 考虑这个设置:

app.js app.js

export class App {
    configureRouter(config: RouterConfig, router: Router): void {
        config.map([ {
            route: '',
            name: 'home',
            viewPorts: {
                left: { moduleId: 'module1' },
                right: { moduleId: 'module1' },
            }
        }]);
    }
}

app.html app.html

<template>
    <router-view name="left"></router-view>
    <router-view name="right"></router-view>
</template>

module1.js module1.js

export class Module1 {
    activate(params: Object, routeConfig: Object, instruction: NavigationInstruction): void {
        //which view port am I being rendered in?
    }
}

My solution was to lookup the navigation instruction's viewport instruction object and compare if it is the very same object instance. 我的解决方案是查找导航指令的视口指令对象,并比较它是否是完全相同的对象实例。 I've created a convinient method for doing so. 我已经创建了一个方便的方法。

navigation-instruction-extension.js 导航指令extension.js

import {NavigationInstruction} from 'aurelia-router';

NavigationInstruction.prototype.viewPortFor = function(viewModelInstance: Object): string {
    for (let key in this.viewPortInstructions) {
        let vpi = this.viewPortInstructions[key];
        if (vpi.component.viewModel === viewModelInstance)
            return key;
    }
    return undefined;
}

module1.js module1.js

import 'navigation-instruction-extension.js';

export class Module1 {
    activate(params: Object, routeConfig: Object, instruction: NavigationInstruction): void {
        instruction.viewPortFor(this); //returns either 'left' or 'right'
    }
}

I've added a new Pull Request, when new version is released, the viewport's name will be accessible through regular lifecycle parameters: 我添加了一个新的Pull Request,当发布新版本时,可以通过常规生命周期参数访问视口的名称:

activate(params: Object, routeConfig: Object, instruction: NavigationInstruction): void {
    routeConfig.currentViewPort //the name of current viewport
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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