Skip to content

Instantly share code, notes, and snippets.

@oakfang
Last active May 1, 2016 13:08
Show Gist options
  • Save oakfang/4c17665aff1e5572ce91f732ddbd65db to your computer and use it in GitHub Desktop.
Save oakfang/4c17665aff1e5572ce91f732ddbd65db to your computer and use it in GitHub Desktop.
Use Xain for elements
'use strict';
const {reactive, link, pipe, observable, observe} = require('xain');
function Xain(state) {
class _Xain {
constructor(props={}) {
const reaction = this.constructor.reaction || (() => ({}));
this.props = reactive(Object.assign({}, reaction(state), props));
observe(this.props, this.__renderToScreen.bind(this));
this.__renderToScreen();
}
__renderToScreen() {
console.log(this.render());
}
render() {
return '';
}
}
return _Xain
}
const state = observable({
firstName: 'Foo',
lastName: 'Bar',
age: 5
})
class Foo extends Xain(state) {
static reaction(state) {
return {
name: link(state, ({firstName, lastName}) => firstName + ' ' + lastName),
age: pipe(state, 'age')
}
}
render() {
const {name, age} = this.props;
return `<div>${name} - ${age}</div>`;
}
}
let x = new Foo()
state.firstName = 'Rawr';
state.firstName = 'Rawr';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment