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?