Skip to content

Instantly share code, notes, and snippets.

@priyajeet
Last active August 29, 2015 14:19
Show Gist options
  • Save priyajeet/c2731789d3a688ee85a9 to your computer and use it in GitHub Desktop.
Save priyajeet/c2731789d3a688ee85a9 to your computer and use it in GitHub Desktop.
T3/React usage rough hack
// todo-list module
Application.addModule('todo-list', function(context) {
var xhr;
var TodoList = React.createClass({
render: function() {
return (
<div>
<TodoItems />
</div>
);
}
});
function render() {
React.render(
<TodoList />,
context.getElement()
);
}
return {
messages: [
'todostatuschange', // message sent by a todo about its state
...
],
init: function() {
xhr = context.getService('xhr');
// some initial render
},
onmessage: function(name, data) {
switch (name) {
case 'todostatuschange':
xhr.post(data).then(render);
break;
...
}
}
};
});
// A todo item wrapper component
Application.addService('todo-items', function(application) {
// Get the Todo component
var Todo = application.getService('todo');
var TodoItems = React.createClass({
render: function() {
return (
<div>
<Todo />
... bunch of todos ...
<Todo />
</div>
);
}
});
return TodoItems;
});
// A todo component
Application.addService('todo', function(application) {
var Todo = React.createClass({
updateTodo: function(event) {
// do some stuff
application.broadcast('todostatuschange', { ... data to send to the server ... });
},
render: function() {
return (
<div onClick={this.updateTodo}>DOM for a todo</div>
);
}
});
return Todo;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment