Skip to content

Instantly share code, notes, and snippets.

@dinks
Created September 23, 2015 22:07

Revisions

  1. dinks created this gist Sep 23, 2015.
    9 changes: 9 additions & 0 deletions budget_app.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    var BudgetApp = React.createClass({
    mixins: [Reflux.connect(EntryStore, 'entries')],

    render: function() {

    return (
    );
    }
    });
    5 changes: 5 additions & 0 deletions entry_actions.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    var EntryActions = Reflux.createActions([
    "entryCreate",
    "entryUpdate",
    "entryDestroy"
    ]);
    19 changes: 19 additions & 0 deletions entry_row.jsx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@

    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>
    );
    }
    });
    35 changes: 35 additions & 0 deletions entry_store.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    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);
    }
    });