4

当我单击按钮#btn1#btn2保存时,控制台会显示:

Uncaught Error: save is not a function
    getFunction @ aurelia-binding.js:1971
    evaluate @ aurelia-binding.js:1565
    callSource @ aurelia-binding.js:4989
    (anonymous function) @ aurelia-binding.js:5013
    handleDelegatedEvent @ aurelia-binding.js:3211

但是外部按钮#btn3工作正常。我也尝试$parent.save()过,#btn2但它不起作用。任何想法?

应用程序.html

<create-location contact.two-way="contact" working-time.two-way="workingTime">
    <require from="dist/component/working-time"></require>
    <working-time title="Working Time" view-model.ref="workingTime"></working-time>
    <require from="dist/component/contact"></require>
    <contact title="Contact" phone="123" email="user@example.com" fax="123456" view-model.ref="contact"></contact>

    <button id="btn1" type="button" click.delegate="save()">Save (=>error<=)</button>
    <button id="btn2" type="button" click.delegate="$parent.save()">Save (=>error also<=)</button>
</create-location>

创建-location.html

<template>
    <button id="btn3" type="button" click.delegate="save()">Save (=>it works<=)</button>
    <content></content>
</template>

创建-location.js

import {bindable} from 'aurelia-framework'

export class CreateLocationCustomElement {
    @bindable contact;
    @bindable workingTime;

    save() {
        alert("save");
    }
}

更新 我尝试了法比奥的建议并且它有效

另一个问题:看看aurelia dialog,他们在元素testDelegate内部调用 view-model 的函数,<content>类似于我的情况。他们不使用view-model.ref. 我查看了源代码,但我不知道他们在哪里处理该调用。也许我错过了一些观点或者这里有约定。有谁知道他们是怎么做到的?

4

1 回答 1

1

您可以使用 将创建位置的视图模型放在一个属性中,view-model.ref然后使用该属性调用save(). 像这样:

<create-location view-model.ref="createLocationVM" contact.two-way="contact" working-time.two-way="workingTime">
    <require from="dist/component/working-time"></require>
    <working-time title="Working Time" view-model.ref="workingTime"></working-time>
    <require from="dist/component/contact"></require>
    <contact title="Contact" phone="123" email="user@example.com" fax="123456" view-model.ref="contact"></contact>

    <button id="btn1" type="button" click.delegate="createLocationVM.save()">Save (=>error<=)</button>

</create-location>

不过,我认为重新创建整个组件会更好。不需要<require>在组件的内容中使用,也许您可​​以将第二个保存按钮放在创建位置的视图中。

于 2016-06-03T13:24:55.673 回答