Aurelia-仅HTML自定义元素的内联定义
Aurelia - Inline definition of HTML-only custom element
我的Aurelia视图模型中有一个递归对象,看起来像这样:
Class BottomlessPit {
Name: string = '';
MorePits: BottomlessPit[] = null;
}
因此,我想在Aurelia视图中使用递归模板。它只会在一个地方使用,所以我宁愿使用模板文字。以下是一些不起作用的伪代码:
<template name="pit">
<li>
${Name}
<compose view.bind="pit" repeat.for="subpit of MorePits"></compose>
</li>
</template>
这是Aurelia的特征吗?
好吧,这有点伤了我的头,但这里有一种方法可以定义仅限内联html的自定义元素。。。
https://gist.run?id=11ac077048cab0ad9979
app.html
<template>
<h1>Aurelia - Inline Custom Elements</h1>
<template name="person-element" bindable="person">
<h3>${person.name}</h3>
<person-element repeat.for="parent of person.parents" person.bind="parent"></person-element>
</template>
<template name="hello" bindable="message">
${message}
</template>
<person-element person.bind="kid"></person-element>
<hello message="hello world"></hello>
</template>
app.js
export class App {
kid = {
name: 'North West',
parents: [
{
name: 'Kanye West',
parents: []
},
{
name: 'Kim Karsomething',
parents: []
}
]
};
}
main.js
import {relativeToFile} from 'aurelia-path';
import {Origin} from 'aurelia-metadata';
import {TemplateRegistryEntry, TemplateDependency} from 'aurelia-loader';
// override the TemplateRegistryEntry's "template" property, adding
// logic to process inline custom elements (eg <template name="foo">)
let templateDescriptor = Object.getOwnPropertyDescriptor(TemplateRegistryEntry.prototype, 'template');
Object.defineProperty(TemplateRegistryEntry.prototype, 'standardTemplate', templateDescriptor);
Object.defineProperty(TemplateRegistryEntry.prototype, 'template', {
get: function get() {
return this.standardTemplate;
},
set: function set(value) {
// hand off to the standard template property...
this.standardTemplate = value;
let address = this.address;
// loop through each of the inline custom elements and register
// them as dependencies.
let namedTemplates = value.content.querySelectorAll('template[name]:not([name=""])');
for (var i = 0, ii = namedTemplates.length; i < ii; ++i) {
let current = namedTemplates[i];
let name = current.getAttribute('name');
let id = relativeToFile(`./${name}.html`, address); // potential for collision but putting in subfolder would be weird if the inline element had it's own <require> elements
// give the loader the template html
System.set(
id + '!' + System.normalizeSync('text'),
System.newModule({ __useDefault: true, default: current.outerHTML}));
// register the dependency
this.dependencies.push(new TemplateDependency(id, name));
// remove the inline template element from the template
current.parentNode.removeChild(current);
}
}
});
export function configure(aurelia) {
aurelia.use
.standardConfiguration()
.developmentLogging();
aurelia.start().then(() => aurelia.setRoot());
}
相关文章:
- Webdriver.io pageObject模式-通过传递参数来定义元素选择器
- 如何使用javascript独立地为聚合物自定义元素的每个实例的lightdom设置样式
- 注册后是否可以定义HTML5自定义元素中的生命周期回调
- 如何在 Aurelia(奥雷利亚)中访问自定义元素中的变量
- Javascript 向影子根中的自定义元素添加函数
- 在自定义元素中扩展 HTMLCanvasElement 问题
- 聚合物 - 访问自定义元素内的 DOM 输入元素
- 在具有CONTENT标记的自定义元素中使用SELECT标记时出现问题
- 聚合物种子-自定义元素依赖关系
- 自定义元素在JavaScript中创建
- 访问嵌套Polymer自定义元素中的JavaScript方法
- 当.js和.html不在同一文件夹中时,如何在Aurelia中创建自定义元素
- 自定义元素之间的聚合物双向结合[包括示例]
- 从 React 中的自定义元素中修改属性
- 聚合物:创建一个“;一般的“;自定义元素
- Aurelia-仅HTML自定义元素的内联定义
- 手动编译由 aurelia 中的其他自定义元素组成的 dom 元素
- 为什么我们必须注册一个自定义元素
- 聚合物自定义元素和聚合物手势
- 在 aurelia 中使用 repeat.for 绑定自定义元素的正确方法是什么