最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Create a custom event for a Polymer Custom-Element - Stack Overflow

programmeradmin1浏览0评论

I've been trying to create an event to be triggered given certain conditional (more general case), but I have not found a way, at least not in the official documentation of Polymer for handling events

.5/articles/munication.html

The custom element listen to another event, so he expects a condition is met to throw the internal event.

This is a little descriptive example:

Index.html

...
<my-element></my-element>
...
<script>
     var ele = document.querySelector("my-element");
     ele.addEventListener("myCustomEvent",
                 function(){
                     // some action
                     ...
             })
</script>

This code describes who will be listening to the event (myCustomEvent) and an action is detonated.


The other hand es the implementation of the custom element

<polymer-element name="my-element">
  <template>
      <other-custom-element id="foo"></other-custom-element>
  </template>
  <script>
   Polymer("my-element", {
        ready: function(){
        var foo = this.$.foo;
        foo.addEventListener("AnotherEvent" , function(){
            ...
            if(condition){
                ...
                // in this part I need to fire the event 
                // "myCustomEvent"
            }
        })
        }
    })
  </script>
</polymer-element>

My problem is to fire the event when the condition is done, And listen that event outside (in index.html)

References

  1. .5/articles/munication.html
  2. .5/docs/polymer/polymer.html#fire

I've been trying to create an event to be triggered given certain conditional (more general case), but I have not found a way, at least not in the official documentation of Polymer for handling events

https://www.polymer-project/0.5/articles/munication.html

The custom element listen to another event, so he expects a condition is met to throw the internal event.

This is a little descriptive example:

Index.html

...
<my-element></my-element>
...
<script>
     var ele = document.querySelector("my-element");
     ele.addEventListener("myCustomEvent",
                 function(){
                     // some action
                     ...
             })
</script>

This code describes who will be listening to the event (myCustomEvent) and an action is detonated.


The other hand es the implementation of the custom element

<polymer-element name="my-element">
  <template>
      <other-custom-element id="foo"></other-custom-element>
  </template>
  <script>
   Polymer("my-element", {
        ready: function(){
        var foo = this.$.foo;
        foo.addEventListener("AnotherEvent" , function(){
            ...
            if(condition){
                ...
                // in this part I need to fire the event 
                // "myCustomEvent"
            }
        })
        }
    })
  </script>
</polymer-element>

My problem is to fire the event when the condition is done, And listen that event outside (in index.html)

References

  1. https://www.polymer-project/0.5/articles/munication.html
  2. https://www.polymer-project/0.5/docs/polymer/polymer.html#fire
Share Improve this question edited Oct 22, 2020 at 7:03 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Jun 12, 2015 at 1:48 ljofreljofre 3246 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

Try this. (Note: changed event names to be case-insensitive ['AnotherEvent' -> 'another-event'] because the HTML is happier this way):

<polymer-element name="my-element">
  <template>
      <other-custom-element on-another-event="{{doAnotherEvent}}"></other-custom-element>
  </template>
  <script>
   Polymer("my-element", {
      doAnotherEvent: function(){
        if (condition){
          // in this part I need to fire the event 
          // "my-custom-event"
          this.fire('my-custom-event');
        }
      }
    });
  </script>
</polymer-element>
发布评论

评论列表(0)

  1. 暂无评论