I know this is an old thread, but since more people can get here, like me, see below a correct and updated answer compatible with Polymer 1.x (Dirk Grappendorf's answer was quite correct, BUT BROKEN):
To create a Polymer element:
<dom-module id="module-name"> <style> Enter your CSS style here </style> <template> <paper-button raisedButton id="test" link="http://twitter.com" on-tap="goLink">Click</paper-button> </template> <script> Polymer({ is: 'module-name', goLink: function(e) { window.location.href = e.target.getAttribute('link'); } }); </script> </dom-module>
To create an automatic snap template:
<template id="bind" is="dom-bind"> <paper-button raisedButton id="test" link="http://twitter.com" on-tap="goLink">Click</paper-button> </template> <script> var bind = document.querySelector('#bind'); bind.goLink = function(e) { window.location.href = e.target.getAttribute('link'); }; </script>
Or using listeners:
<paper-button raisedButton id="test" link="http://twitter.com">Click</paper-button> <script> var button = document.getElementById('test'); button.addEventListener('tap', function(e) { window.location.href = e.target.getAttribute('link'); }); </script>
Button paper and paper factory work the same way.
What to choose? I would choose to create an item if I am going to reuse this button for more projects or in other parts of the same project; automatic linking if I do not reuse this code, but my interface has many events to process (it is easier to encapsulate the entire user interface in one dom-bind template and create all the handlers needed using the bind.handlerName syntax than to create listeners for all user interface elements that trigger events - and one listener for each event fires the same element). And the thid variant, listeners, only if what I want cannot be executed by other parameters (for example, when user events are triggered), or only 1 or 2 events are triggered on the page (in this case the code seems to be shorter than the other options).
VinΓcius A. Jorge
source share