バインド可能な列属性に基づいてテンプレートを動的にレンダリングする、行ベース (テーブルのような) カスタム要素があります。行テンプレートを文字列で構成し、ViewCompiler、ViewFactory、およびViewを使用してレンダリングします。
import {inject} from 'aurelia-dependency-injection';
import {bindable, ViewCompiler, ViewResources} from 'aurelia-templating';
@inject(ViewCompiler, ViewResources)
export class MyDynamicGrid {
@bindable columns: any[];
@bindable rows: any[];
constructor(viewCompiler: ViewCompiler, viewResources: ViewResources) {
const template = '<template><custom-element></custom-element></template>'; //this is rather complex in practice
viewResources.registerElement('custom-element', /* HtmlBehaviorResource? */);
this._viewFactory = viewCompiler.compile(template, viewResources);
}
_render() : void {
const view = this._viewFactory.create(/* some container */);
view.bind(someContext, someOverrideContext);
//attach view to the DOM
}
}
カスタム テンプレートに標準の HTML 要素が含まれるまで、これは問題なく機能します。カスタム要素をテンプレートに配置し始めると、機能しなくなります。HTML は引き続きレンダリングされますが、カスタム要素の動作は Aurelia によって関連付けられていません。
すべてのカスタム要素を使用するには、「登録」する必要があることを認識しています。「通常の登録」は、 を介してビューで<require>
、またはビューモデル@viewResources
で、またはグローバルに登録することで発生します。
ただし、この特定のケースでは、注入されViewCompiler
た はビューモデルの親のビュー リソースのみを継承します。私の質問は次のとおりです。追加のビューリソースを登録するにはどうすればよいですか? ViewCompiler
のcompile
メソッドの 2 番目のパラメーターは認識していますが、機能させることができませんでした。グローバルに登録する場合、それを機能させることができた唯一の方法です。
注: この質問は、ビュー リソースの登録に焦点を当てています。動的レンダリングは問題なく動作します