Polymer JS将函数从一个组件传递到另一个组件?

时间:2015-06-26 21:23:04

标签: javascript jquery mobile web polymer

组件A:

<polymer-element name="componentA" extends="core-ajax" attributes="onComplete">
<script>

Polymer('componentA',{
    requestCompleted: function(){
         this.onComplete();
    }
});

组件B:

<polymer-element name="componentB">
<template>
    <componentA method="GET" onComplete="{{myCallbackFunction()}}"></componentA>
</template>
<script>
    Polymer('componentB',{

    name: 'doug',
    myCallbackFunction: function(){
         alert("this works!");
         this.name='mike';
    }
});
</script>

所以我的问题是我可以在componentA的属性中传递myCallbackFunction并且它会执行它,警告&#34;这有效!&#34;。但是,componentB名称仍设置为&#39; doug&#39;。所以我可以成功传递函数但是如何在该函数中访问组件B的变量?

1 个答案:

答案 0 :(得分:2)

改为使用事件。

组件A:

<polymer-element name="componentA" extends="core-ajax">
<script>

Polymer('componentA',{
    requestCompleted: function(){
      // core-ajax already fires an event, but for demonstration
      this.fire('complete');
    }
});

组件B:

<polymer-element name="componentB">
<template>
    <componentA method="GET" on-complete="{{myCallbackFunction}}"></componentA>
</template>
<script>
    Polymer('componentB',{

    name: 'doug',
    myCallbackFunction: function(e){
         alert("this works!");
         this.name='mike';
    }
});
</script>