<!-- Demo on how to implement global variables with Polymer 1.0 -->
<script src="https://rawgit.com/webcomponents/webcomponentsjs/master/webcomponents-lite.js"></script>
<link rel="import" href="https://rawgit.com/Polymer/polymer/master/polymer.html">
<!-- Originally based on http://stackoverflow.com/questions/30849816/polymer-1-0-global-variables -->
<dom-module id="app-data">
</dom-module>
<script>
(function () {
var instances = [];
var vars = Object.create(Polymer.Base);
Polymer({
is: 'app-data',
properties: {
data: {
type: Object,
value: '',
notify: true,
readonly: false,
observer: '_data_changed'
},
key: String
},
created: function () {
key = this.getAttribute('key');
if (!key){
console.log(this);
throw('app-data element requires key');
}
instances.push({key:key, instance:this});
},
detached: function () {
key = this.getAttribute('key');
var i = instances.indexOf({key:key, instance:this});
if (i >= 0) {
instances.splice(i, 1);
}
},
_data_changed: function (newvalue, oldvalue) {
key = this.getAttribute('key');
if (!key){
throw('_data_changed: app-data element requires key');
}
vars.set(key, newvalue);
// notify the instances with the correct key
for (var i = 0; i < instances.length; i++)
{
if(instances[i].key == key)
{
instances[i].instance.notifyPath('data', newvalue);
}
}
}
});
})();
</script>
<!-- Usage example -->
<!-- Element definitions -->
<!-- Input Element -->
<dom-module id="input-element">
<template>
<app-data id="dataFirstName01" key="fName" data="{{firstname}}" ></app-data>
<app-data id="dataLastName01" key="lName" data="{{lastname}}" ></app-data>
<h4>Input-Element</h4>
<div>First Name: <span>{{firstname}}</span> Last Name: <span>{{lastname}}</span></div>
<input type="text" name="input_fName" placeholder="First Name" value="{{firstname::input}}"></input>
<input type="text" name="input_lName" placeholder="Last Name" value="{{lastname::input}}"></input>
<div>Local First Name: </div>
<input type="text" id="inputlocalname" placeholder="Local Name" value="Alice"></input>
<button id="but" on-click="copy">Copy to global</button>
</template>
</dom-module>
<script>
Polymer({
is:'input-element',
copy: function () {
this.firstname = this.$.inputlocalname.value;
console.log(this.data02);
}
});
</script>
<!-- Output element -->
<dom-module id="output-element" >
<template>
<app-data id="data012" key="fName" data="{{data1}}" ></app-data>
<app-data id="data012" key="lName" data="{{data2}}" ></app-data>
<h4>Output-Element</h4>
<div>First Name: <span>{{data1}}</span></div>
<div>Last Name: <span>{{data2}}</span></div>
</template>
</dom-module>
<script>Polymer({is:'output-element'});</script>
<!-- Body -->
<body>
<div>
<template is="dom-bind">
<app-data key="fName" data="{{firstName}}" ></app-data>
<app-data key="lName" data="{{lastName}}" ></app-data>
<h3>Name in Title: <span>{{firstName}}</span> <span>{{lastName}}</span></h3>
</template>
<input-element id="first-input"></input-element>
<input-element id="second-input"></input-element>
<output-element></output-element>
</div>
<p></p>
<template is="dom-bind">
<app-data key="fName" data="{{data1}}" ></app-data>
<h3>Edit on top-level: </h3>
<input type="text" name="input_fName" placeholder="First Name"
value="{{data1::input}}"></input>
</template>
</body>
Output
You can jump to the latest bin by adding /latest
to your URL
Keyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |