如何使用Aurelia

时间:2017-11-27 10:51:03

标签: javascript aurelia aurelia-binding

我们将所有页面内容和博客帖子等存储在WordPress中,使用其API在我们的Aurelia应用程序中呈现数据。这很好用;

<div class="excerpt" innerhtml.bind="post.excerpt.rendered"></div>

作者现在希望能够链接到弹出窗口或使用其博客文章中的route-href或其他自定义Aurelia属性,但是使用innerhtml.bind添加到页面的代码无法通过奥里利亚。

我喜欢普通的<a href="...">“只在Aurelia工作” - 但我们有很多自定义属性(如<button popup="name-of-popup">...</button>,作者无法使用。

我们如何克服这个?

编辑:根据@bigopon的评论,我已经开始了一些事情,但仍然无法让它发挥作用。要么我在搜索方面很糟糕,要么文档在TemplatingEngine.enhance()方法上有点缺乏,但我尝试创建这样的自定义属性:

import {Aurelia, inject, TemplatingEngine} from 'aurelia-framework';

@inject(Element, Aurelia, TemplatingEngine)
export class AureliaEnhanceCustomAttribute {
    constructor (el, aurelia, templatingEngine) {
        this.el = el;
        this.aurelia = aurelia; // NOTE: I've never done this before - is it even correct?
        this.templatingEngine = templatingEngine;
    }

    attached () {
        this.el.innerHTML = this.value;

        // NOTE: Without checking for this we get an endless loop of attached() calls
        if (!this.el.classList.contains('au-target')) {
            this.templatingEngine.enhance({
                element: this.el,
                container: Aurelia.container, // NOTE: I'm guessing here
                resources: Aurelia.resources, // NOTE: Same here, but I need all my global resources to be available inside the enhanced element too
                bindingContext: {} // NOTE: Not sure what to pass in here at all.. :/
            });
        }
    }
}

我正在使用它:

<div aurelia-enhance.bind="exampleContent"></div>

其中exampleContent是从API调用中获取的字符串,如下所示:'<my-custom-element></my-custom-element><button my-custom-attribute="some-value">Log in</button>'

1 个答案:

答案 0 :(得分:2)

你走在正确的轨道上。几乎没有什么可以考虑的事情

  • bindingContext / overrideContext:您可以通过挂钩自定义属性的bind生命周期来获得这两个。因此,您可以将它们传递给enhance指令(仅需要bindingContext,但是传递两者更好,有助于遍历范围)。关于bindingContext,99%它将是您所在的视图模型,但您始终可以使用其他对象。在这种情况下,this(自定义属性视图模型)是正确的。

  • 资源:取决于您希望TemplatingEngine.prototype.enhance返回的视图的资源范围。传递全局Aurelia实例resources将不会产生它所驻留的自定义元素的本地资源范围。为了与属性注释的元素具有相同的资源,请挂钩created属性的生命周期,将第一个参数存储为owningView。这是包含该属性的自定义元素的视图。然后,您可以owningView.resources

  • 访问其资源
  • 清理:TemplatingEngine.prototype.enhance返回View,您还需要在属性生命周期中将此引用存储到detachedunbind

    < / LI>
  • 消毒

示例:https://gist.run/?id=9dd32bc8a772526ae527f593e26b275b

上面的要点是继承/增强的一个例子,我在话语中回答了另一个问题。您可以查看select-field以查看更多示例。它与你在那里所做的类似。

P / S:如果您对该解决方案感到满意,可以考虑写一篇博文/文章,或者在Aurelia Discourse论坛上开设一个主题,以帮助那些也可能在那里挣扎的人。

更新示例:

import {Aurelia, inject, TemplatingEngine, DOM} from 'aurelia-framework';

@inject(Element, TemplatingEngine)
export class AureliaEnhanceCustomAttribute {
    constructor (el, aurelia, templatingEngine) {
        this.el = el;
        this.templatingEngine = templatingEngine;
    }

    // Custom attribute doesn't have its own view
    // Only the view of the custom element that owns it
    created(owningElementView, thisView) {
        this.owningElementView = owningElementView;
    }

    bind(bindingContext, overrideContext) {
        this.bindingContext = bindingContext;
        this.overrideContext = overrideContext;
    }

    attached () {
        this.el.innerHTML = this.value;

        // NOTE: Without checking for this we get an endless loop of attached() calls

        if (!this.el.classList.contains('au-target')) {
            this.dynamicView = this. this.templatingEngine.enhance({
                element: this.el,
                // we have two choices here, either the container of owning element, or this attribute
                // Lets go with the element, since it propbably has everything we need
                container: this.owningElementView.container,
                // the resources has information about its parent,
                // So we just need the resources of the element containing this attribute
                resources: this.owningElementView.resources,
                // Or this.bindingContext (same)
                bindingContext: this,
                // this helps travel up
                overrideContext: this.overrideContext
            });
        }
    }

    detached() {
        this.dynamicView.detached();
    }

    unbind() {
        if (this.dynamicView) {
            this.dynamicView.unbind();
        }
    }
}