Last active
May 1, 2016 13:08
-
-
Save oakfang/4c17665aff1e5572ce91f732ddbd65db to your computer and use it in GitHub Desktop.
Use Xain for elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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