javascript - Aurelia - Inline definition of HTML-only custom element - Stack Overflow

I have a recursive object in my Aurelia view model that looks like this:Class BottomlessPit {Name: stri

I have a recursive object in my Aurelia view model that looks like this:

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

Therefore, I'd like to use a recursive template in my Aurelia view. It will only be used in one place, so I would rather use a template literal. Here's some pseudocode that doesn't work:

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

Is this a feature of Aurelia?

I have a recursive object in my Aurelia view model that looks like this:

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

Therefore, I'd like to use a recursive template in my Aurelia view. It will only be used in one place, so I would rather use a template literal. Here's some pseudocode that doesn't work:

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

Is this a feature of Aurelia?

Share Improve this question edited Oct 12, 2016 at 22:36 Jeremy Danyow 26.4k12 gold badges90 silver badges135 bronze badges asked Mar 24, 2016 at 1:22 Matthew James DavisMatthew James Davis 12.3k7 gold badges63 silver badges91 bronze badges 1
  • You can do what are are trying by using the template inside of itself but the browser will crash with stack overflow – PW Kad Commented Mar 24, 2016 at 2:32
Add a ment  | 

1 Answer 1

Reset to default 9

OK this hurt my head a little bit but here's a way to enable defining inline html-only custom elements...

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());
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745216132a4617030.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信