Skip to content

Instantly share code, notes, and snippets.

@mikaelbr
Forked from torgeir/quiescent-for-js.js
Last active August 29, 2015 14:07
Show Gist options
  • Save mikaelbr/92bfd8d710470f06fc1c to your computer and use it in GitHub Desktop.
Save mikaelbr/92bfd8d710470f06fc1c to your computer and use it in GitHub Desktop.
var React = require('react');
var EventEmitter = require('events').EventEmitter;
function component (mixins, render) {
if (typeof mixins === 'function') {
render = mixins;
mixins = [];
}
if (!Array.isArray(mixins)) {
mixins = [mixins];
}
var Component = React.createClass({
mixins: mixins,
render: function () {
return render.call(this, this.props.cursor, this.props.statics);
},
shouldComponentUpdate: function (nextProps) {
return this.props.cursor != nextProps.cursor;
}
});
return function (cursor, statics) {
return Component({ cursor: cursor, statics: statics });
};
}
// use
var Heading = modul.exports = component([{
handleClick: function (statics) {
return function (e) {
e.preventDefault();
this.setProps({
foo: true
});
statics.events.emit('delete');
};
},
setProps: function (obj) {
this.props.cursor.update(function (state) {
return state.mergeDeep(obj);
});
}
}], function (cursor, statics) {
return React.DOM.text(null,
cursor.get('text'),
React.DOM.text({ onclick: this.handleClick(statics) }, 'x')
);
});
var headingEvents = new EventEmitter();
headingEvents.on('delete', function () {
console.log('got delete', h);
});
var h = Heading({ text: 'some text' }, { events: headingEvents, sharedState: {} });
$ = 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