Skip to content

Instantly share code, notes, and snippets.

@balloob
Last active March 1, 2016 05:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balloob/a54631ed08784ccd9a23 to your computer and use it in GitHub Desktop.
Save balloob/a54631ed08784ccd9a23 to your computer and use it in GitHub Desktop.
Behavior to use Nuclear JS data in Polymer
<!--
Behavior to connect your NuclearJS app to Polymer.
Add key 'bindNuclear' to your property with as value a
valid NuclearJS getter.
Adapted from the NuclearJS ReactJS mix-in:
https://github.com/jordangarcia/nuclear-react-mixin
-->
<dom-module id='nuclear-example'>
<template>
Look, it's data from NuclearJS: <span>[[loaded]]</span>!
</template>
</dom-module>
<script>
function NuclearObserver(reactor) {
return {
attached: function() {
var component = this;
this.__unwatchFns = Object.keys(component.properties).reduce(
function(unwatchFns, key) {
if (!('bindNuclear' in component.properties[key])) {
return unwatchFns;
}
var getter = component.properties[key].bindNuclear;
if (!getter) {
throw 'Undefined getter specified for key ' + key;
}
component[key] = reactor.evaluate(getter);
return unwatchFns.concat(reactor.observe(getter, function(val) {
component[key] = val;
}));
}, []);
},
detached: function() {
while (this.__unwatchFns.length) {
this.__unwatchFns.shift()();
}
},
};
}
// assuming variable 'reactor' is your NuclearJS reactor.
// assuming variable 'bootstrapGetter.isDataLoaded' is a NuclearJS getter
// that returns a boolean.
Polymer({
is: 'nuclear-example',
behaviors: [NuclearObserver(reactor)],
properties: {
loaded: {
type: Boolean,
bindNuclear: bootstrapGetters.isDataLoaded,
},
},
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment