Last active
August 29, 2015 14:07
-
-
Save torgeir/1413303d23887f926312 to your computer and use it in GitHub Desktop.
Naive quiescent for js.
This file contains 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
var React = require('react'); | |
var EventEmitter = require("events").EventEmitter; | |
function component (fn) { | |
var Component = React.createClass({ | |
render: function () { | |
return fn.call(this, this.props.value, this.props.statics); | |
}, | |
shouldComponentUpdate: function (nextProps) { | |
return this.props.value != nextProps.value; | |
} | |
}); | |
return function (value, statics) { | |
return Component({ value: value, statics: statics }); | |
}; | |
}; | |
// use | |
var Heading = component(function (value, statics) { | |
function onDelete () { | |
statics.events.emit('delete', 'someone clicked me'); | |
} | |
var deleteButton = React.DOM.text({ onClick: onDelete }, 'x'); | |
return React.DOM.text({}, | |
value.text, | |
" ", | |
deleteButton); | |
}); | |
var headingEvents = new EventEmitter(); | |
var h = Heading({ text: 'some text' }, { events: headingEvents }); | |
headingEvents.on('delete', function () { | |
console.log('got delete', h); | |
}); | |
$ = document.querySelector.bind(document); | |
React.renderComponent(h, $('.app')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment