Skip to content

Instantly share code, notes, and snippets.

View flarnie's full-sized avatar

Flarnie Marchan flarnie

View GitHub Profile
@flarnie
flarnie / ThreadStore.js
Created December 9, 2014 00:55
Flux ChatApp Sample 1
// The case statement documents which actions this store listens to
// ...
ThreadStore.dispatchToken = ChatAppDispatcher.register(function(payload) {
var action = payload.action;
switch(action.type) {
case ActionTypes.CLICK_THREAD:
_currentID = action.threadID;
_threads[_currentID].lastMessage.isRead = true;
@flarnie
flarnie / TodoApp2.react.js
Created December 9, 2014 00:50
Flux TodoApp Sample 8
// The component listens for changes and calls the '_onChange' callback
// ...
var TodoApp = React.createClass({
getInitialState: function() {
return getTodoState();
},
componentDidMount: function() {
TodoStore.addChangeListener(this._onChange);
@flarnie
flarnie / TodoStore2.js
Created December 9, 2014 00:47
Flux TodoApp Sample 7
// TodoStore emits a 'change' event after handling the action.
// ...
// Register to handle all updates
AppDispatcher.register(function(payload) {
var action = payload.action;
var text;
switch(action.actionType) {
case TodoConstants.TODO_CREATE:
text = action.text.trim();
@flarnie
flarnie / TodoStore1.js
Created December 9, 2014 00:47
Flux TodoApp Sample 6
// The TodoStore has registered a callback for the 'TODO_CREATE' action.
// ...
/**
* Create a TODO item.
* @param {string} text The content of the TODO
*/
function create(text) {
// Hand waving here -- not showing how this interacts with XHR or persistent
// server-side storage.
// Using the current timestamp + random number in place of a real id.
@flarnie
flarnie / AppDispatcher.js
Created December 9, 2014 00:44
Flux TodoApp Sample 5
// The 'handleViewAction' dispatches the action to all stores.
// ...
var Dispatcher = require('flux').Dispatcher;
var assign = require('object-assign');
var AppDispatcher = assign(new Dispatcher(), {
/**
* A bridge function between the views and the dispatcher, marking the action
* as a view action. Another variant here could be handleServerAction.
@flarnie
flarnie / TodoActions.js
Created December 9, 2014 00:43
Flux TodoApp Sample 4
// The 'create' method creates an action of type 'TODO_CREATE'
// ...
var TodoActions = {
/**
* @param {string} text
*/
create: function(text) {
AppDispatcher.handleViewAction({
actionType: TodoConstants.TODO_CREATE,
@flarnie
flarnie / Header2.react.js
Created December 9, 2014 00:42
Flux TodoApp Sample 3
// The '_onSave' callback calls the 'TodoActions' method to create an action
// ...
/**
* Event handler called within TodoTextInput.
* Defining this here allows TodoTextInput to be used in multiple places
* in different ways.
* @param {string} text
*/
_onSave: function(text) {
if (text.trim()){
@flarnie
flarnie / Header1.react.js
Created December 9, 2014 00:41
Flux TodoApp Sample 2
// Saving a new ToDo calls the '_onSave' callback
// ...
var Header = React.createClass({
/**
* @return {object}
*/
render: function() {
return (
<header id="header">
@flarnie
flarnie / TodoApp1.react.js
Created December 9, 2014 00:40
Flux TodoApp Sample 1
// Loading the initial data into the application:
// ...
/**
* Retrieve the current TODO data from the TodoStore
*/
function getTodoState() {
return {
allTodos: TodoStore.getAll(),
areAllComplete: TodoStore.areAllComplete()
};
@flarnie
flarnie / AppDispatcher.js
Created December 9, 2014 00:33
Flux ToDoApp Code Samples
// The 'handleViewAction' dispatches the action to all stores.
// ...
var Dispatcher = require('flux').Dispatcher;
var assign = require('object-assign');
var AppDispatcher = assign(new Dispatcher(), {
/**
* A bridge function between the views and the dispatcher, marking the action
* as a view action. Another variant here could be handleServerAction.