Skip to content

Instantly share code, notes, and snippets.

@dinks
Created September 23, 2015 22:07
Show Gist options
  • Save dinks/4fe731e88a32ce729779 to your computer and use it in GitHub Desktop.
Save dinks/4fe731e88a32ce729779 to your computer and use it in GitHub Desktop.
reflux Stuff
var BudgetApp = React.createClass({
mixins: [Reflux.connect(EntryStore, 'entries')],
render: function() {
return (
);
}
});
var EntryActions = Reflux.createActions([
"entryCreate",
"entryUpdate",
"entryDestroy"
]);
var EntryRow = React.createClass({
_onRemoveClick: function() {
EntryActions.entryDestroy(this.props.entry.id);
},
render: function () {
var entry = this.props.entry;
return (
<tr>
<td>{entry.title}</td>
<td className={entry.amount > 0 ? 'success' : 'danger'}>{entry.amount.toFixed(2)}</td>
<td>{entry.category}</td>
<td>{entry.timestamp.toLocaleFormat()}</td>
<td><a href="#" onClick={this._onRemoveClick}>REMOVE</a></td>
</tr>
);
}
});
var Reflux = require('reflux');
var EntryActions = require('../actions/entry_actions');
var assign = require('object-assign');
var _entries = {};
var EntryStore = Reflux.createStore({
listenables: [EntryActions],
getInitialState: function() {
return _entries;
},
entryCreate: function(title, amount, category) {
var id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36);
_entries[id] = {
id: id,
title: title,
amount: amount,
category: category,
timestamp: (new Date())
};
this.trigger(_entries);
},
entryUpdate: function(id, updates) {
_entries[id] = assign({}, _entries[id], updates);
this.trigger(_entries);
},
entryDestroy: function(id) {
delete _entries[id];
this.trigger(_entries);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment