HTML5 / webcomponents:从模板代码调用原型函数

时间:2015-10-02 22:57:22

标签: javascript html5 web-component

我正在进行一些测试,以便在我创建的单页应用中使用(或不使用)网络组件。

以下是问题的一个示例:



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>

<template id="aTemplate">
    <div style="border:1px solid red">
        <p>text <input type="text"></p>
        <button>ClickMe</button>
    </div>
</template>

<script>
    var Proto = Object.create(HTMLElement.prototype);

    Proto.createdCallback = function () {
        var t = document.querySelector('#aTemplate');
        var clone = document.importNode(t.content, true);
        this.createShadowRoot().appendChild(clone);
    };

    Proto.aFunction = function() {
        alert("proto " + "text value?");
    }

    document.registerElement('x-proto', {prototype: Proto});

    var ProtoChild = Object.create(Proto);

    ProtoChild.createdCallback = function () {
        Proto.createdCallback.call(this)
    };

    ProtoChild.aFunction = function() {
        alert("child " + "text value?");
    }

    document.registerElement('x-proto-child', {
        prototype: Proto
    });

</script>

<x-proto></x-proto>

<x-proto-child></x-proto-child>

</body>
&#13;
&#13;
&#13;

问题是我找不到设置&#34; onclick&#34;的方法。按钮中的处理程序(在模板内)调用方法&#34; aFunction&#34;在使用原型创建的对象中。该方法应该在正确的对象实例中调用,可以访问内部DOM组件,以及原型中的属性和函数。

我尝试了很多东西,(在构建之后使用JS或JQuery绑定事件,使用创建的/附加的回调,a)但是我没有想法。

提前致谢。

编辑:

感谢MinusFour的回答。这一行:

clone.querySelector('button').addEventListener('click', this.aFunction); 

在我试图做的事情中,无论如何,导致(与JQuery相同,用于测试兼容性):

$(this.showButton).on("click", this.aFunction.bind(this));

&#34; bind&#34;使这个&#39; AKA容器,完整组件,JS代码,我需要的。

在这里完成的最后一个例子,有人可能会发现它有用:

&#13;
&#13;
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
</head>
<body>

<template id="aTemplate">
    <div style="border:1px solid darkgray;padding:10px;margin: 10px;">
        <h2 class="theCaption"></h2>
        <p>text <input class="theText" type="text"></p>
        <button class="showButton">Show val</button>
        <button class="closeButton">Close</button>
    </div>
</template>

<script>

    // The .bind method from Prototype.js
    if (!Function.prototype.bind) { // check if native implementation available
        Function.prototype.bind = function () {
            var fn = this, args = Array.prototype.slice.call(arguments),
                    object = args.shift();
            return function () {
                return fn.apply(object,
                        args.concat(Array.prototype.slice.call(arguments)));
            };
        };
    }

    function createProto() {
       $("#spawnPoint").append("<x-proto x-caption='proto'></x-proto>");
    }

    function createChild() {
        $("#spawnPoint").append("<x-proto-child x-caption='a child of proto'></x-proto-child>");
    }

    var Proto = Object.create(HTMLElement.prototype);

    Object.defineProperty(Proto, "x-caption", {value: "no caption"});

    Proto.createdCallback = function () {
        var t = document.querySelector('#aTemplate');
        var clone = document.importNode(t.content, true);
        this.shadowRoot = this.createShadowRoot();
        this.shadowRoot.appendChild(clone);
        $(clone).children("div").append("<p>ssss</p>")
        this.showButton = this.shadowRoot.querySelector('.showButton');
        this.closeButton = this.shadowRoot.querySelector('.closeButton');
        this.shadowRoot.querySelector('.theCaption').textContent = $(this).attr("x-caption");
        this.theText = this.shadowRoot.querySelector('.theText');
        $(this.showButton).on("click", this.aFunction.bind(this));
        $(this.closeButton).on("click", this.close.bind(this));
    };

    Proto.aFunction = function () {
        alert("in proto = " + $(this.theText).val());
    }

    Proto.close = function () {
        $(this).detach();
    }

    document.registerElement('x-proto', {prototype: Proto});

    var ProtoChild = Object.create(Proto);

    ProtoChild.createdCallback = function () {
        Proto.createdCallback.call(this);
    };

    ProtoChild.aFunction = function () {
        alert("overrided in child = " + $(this.theText).val());
    }

    document.registerElement('x-proto-child', {
        prototype: ProtoChild
    });

</script>

<button onclick="javascript:createProto()">Create proto</button>
<button onclick="javascript:createChild()">Create child</button>

<div id="spawnPoint">

</div>

</body>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

我相信您可以添加importedNode(在您的情况下为clone)中的听众。

clone.querySelector('button').addEventListener('click', function(){
  //code logic here
});

你也可能意味着:

document.registerElement('x-proto-child', {
        prototype: ProtoChild
    });

以下是它的样子:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
</head>
<body>

<template id="aTemplate">
    <div style="border:1px solid red">
        <p>text <input type="text"></p>
        <button>ClickMe</button>
    </div>
</template>
<script src="https://cdnjs.cloudflare.com/ajax/libs/webcomponentsjs/0.7.14/webcomponents.min.js"></script>
<script>
    var Proto = Object.create(HTMLElement.prototype);
  
    Proto.createdCallback = function () {
        var t = document.querySelector('#aTemplate');
        var clone = document.importNode(t.content, true);
        clone.querySelector('button').addEventListener('click', this.aFunction);
        this.createShadowRoot().appendChild(clone);
    };

    Proto.aFunction = function() {
        alert("proto " + "text value?");
    }

    document.registerElement('x-proto', {prototype: Proto});

    var ProtoChild = Object.create(Proto);

    ProtoChild.createdCallback = function () {
        Proto.createdCallback.call(this);
        console.log(this.template);
        /*this.template.querySelector('button').addEventListener('click', function(){
          console.log('child');
          });*/
    };

    ProtoChild.aFunction = function() {
        alert("child " + "text value?");
    }

    document.registerElement('x-proto-child', {
        prototype: ProtoChild
    });

</script>

<x-proto></x-proto>

<x-proto-child></x-proto-child>

</body>

答案 1 :(得分:0)

我有一个类似的问题,不久前:

http://www.davevoyles.com/accessing-member-functions-in-polymer/

您是在使用Polymer,还是只使用Web Components?

只要你在聚合物就绪事件中包装你想要调用的函数,你应该很好,并且可以调用聚合物元素中的函数。

  

Polymer解析元素定义并处理其升级   异步。如果您过早地从DOM中获取元素   在它有机会升级之前,你将会处理一个简单的问题   HTMLElement,而不是您的自定义元素。

或者,您可以根据需要查询自定义元素(通过ID,类,attr或元素名称)并获得相同的内容。这是他的例子:http://jsbin.com/qikaya/2/edit

相关问题