On the event binding _ basics in the polymer framework of JavaScript

Source: Internet
Author: User

Since it is a complete set of front-end frameworks, there must be support for event binding. In fact, in the previous example, the use of event binding, but not a separate systematic introduction. Polymer's event idea is to name the event handlers as much as possible and define them on the VM, which I think is deliberately isolating the VM layer.
The following example binds an event to the button and its Shadow DOM host, and both events trigger when the button is clicked.
Run

<script> var polymer = {dom: ' Shadow '}; </script>
<base href= "http://www.web-tinker.com/share/"/>
<link rel= "import" href= "polymer" /polymer.html "/>

<dom-module id=" Demo-test ">
 <template>
  <button on-click=" ClickHandler "> Click (=~ω~=) </button>
 </template>
 <script>
  polymer ({is
   : ' Demo-test ',
   listeners: {
    ' click ': ' ClickHandler '
   },
   clickhandler:function (e) {
    Console.log (E.target);
   } 
  );
 </script>
</dom-module>

<demo-test></demo-test>

Listeners is an event that is used to add events to the current Shadow DOM Host (although I think it has no eggs). The On-* property directly on the DOM element can also bind an event to an element. These methods bind to DOM events, and the object that passes past when the event is triggered is the native event object.
In addition to these event binding methods, which are directly set as attributes, we can also bind events dynamically.
Run

<script> var polymer = {dom: ' Shadow '}; </script>
<base href= "http://www.web-tinker.com/share/"/>
<link rel= "import" href= "polymer" /polymer.html "/>

<dom-module id=" Demo-test ">
 <template>
  <button> Click (=~ω~=) < /button>
 </template>
 <script>
  polymer ({
   is: ' Demo-test ',
   ready:function () {
    //Polymer built-in
    This.listen (this, ' click ', ' ClickHandler ');
    Native
    this.addeventlistener (' click ', This.clickhandler);
   },              
   clickhandler:function (e) {     
    Console.log (e);
   }
  });
 </script>
</dom-module>

<demo-test></demo-test>

Polymer always wants us to name the event handler function, such as its own listen method binds an element not an event handler function, but an event handler function name. Maybe the goal is to isolate the VM and M completely, but I don't like it. But Shadow DOM Host itself is also a native object, so it is also possible to use native AddEventListener directly, but since there is provision within the framework, I do not recommend writing native. Perhaps my thoughts are too low to comprehend the polymer of this design?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.