javascript - Aurelia - 纯 HTML 自定义元素的内联定义

标签 javascript aurelia

我的 Aurelia View 模型中有一个递归对象,如下所示:

Class BottomlessPit {
    Name: string = '';
    MorePits: BottomlessPit[] = null;
}

因此,我想在我的 Aurelia View 中使用递归模板。它只会在一个地方使用,所以我宁愿使用模板文字。这是一些不起作用的伪代码:

<template name="pit">
    <li>
        ${Name}
        <compose view.bind="pit" repeat.for="subpit of MorePits"></compose>
    </li>
</template>

这是 Aurelia 的特性吗?

最佳答案

好吧,这让我有点头疼,但这里有一种方法可以定义内联 html-only 自定义元素...

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: []
      }
    ]
  };
}

ma​​in.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());
}

关于javascript - Aurelia - 纯 HTML 自定义元素的内联定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36191678/

相关文章:

javascript - Jgrowl 喜欢使用 dojo 的通知

javascript - 在 Javascript 中提取 HTML 标题

javascript - Aurelia官方联系人列表教程添加Bootstrap配置后渲染失败

promise - ES6 和 promise 中的变量范围

aurelia - 在 Aurelia 中测试具有可绑定(bind)字段的类

node.js - Aurelia e2e : text. indexOf() 未正确将字符串与 div 中包含的字符串进行比较

aurelia - 在 Aurelia Store 的配置管道步骤中使用 @connectTo 装饰器时,状态始终未定义

javascript - 如何测试输入是否在 if 结构中具有焦点?

javascript - jQuery 通过 'src' 属性查找图像 - 纯 javascript

javascript - 禁用提交,但如果按下后退按钮,请确保其重新启用