2017-02-13 5 views
3

私は2列のレイアウト設定をしていますが、2つの列は非常に似通った機能を持っているため、同じビューモデルが再利用されています。 Hovewerでは、レンダリングはどちら側がレンダリングされているかによって若干異なる場合がありますので、ビューポート情報にどのようにアクセスできるのでしょうか?Aurelia - どのビューポートがレンダリングされているビューモデルですか?

この設定を考えてみましょう:

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

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

module1.js

答えて

2

私の解決策は、ナビゲーション命令のビューポート命令オブジェクトをルックアップし、それが非常に同じオブジェクトインスタンスであるかどうかを比較することでした。私はそうするための納得のいく方法を作りました。

ナビゲーション命令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

import 'navigation-instruction-extension.js'; 

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

もっと良い方法があるはずですが、私はちょうど30分を試​​して、 'activate()'関数に渡されたパラメータで簡単な答えを見つけることができませんでした。 – LStarky

+0

私はこれを調べようとしましたが、これはこれまでのところ最良の解決策です。 – balazska

+0

フレームワークによって 'activate()'のパラメータとして送信されるのが最良です。私はこのためのPRを作成します – balazska

0

私は新しいバージョンがリリースされた新しいプル要求、ビューポートの名前を追加しました定期的なライフサイクルのパラメータでアクセスできます:

activate(params: Object, routeConfig: Object, instruction: NavigationInstruction): void { 
    routeConfig.currentViewPort //the name of current viewport 
} 
関連する問題