Last active
August 29, 2015 14:16
Multiple Dispatch Issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var MessageActions = { | |
loadMessages: function (groupId) { | |
var params = {groupId: groupId}; | |
Dispatcher.send(Constants.messages.LOAD, params); | |
MessageApi.getMessagesForGroup(groupId) | |
.then(function (res) { | |
params.response = res; | |
Dispatcher.send(Constants.messages.LOAD_SUCCESS, params); | |
},function (err) { | |
params.resonse = err; | |
Dispatcher.send(Constants.messages.LOAD_ERROR, params); | |
}) | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getState(){ | |
var thread = ThreadStore.getSelectedThread(); | |
return { | |
thread: thread, | |
messages: MessageStore.getAllForThread(thread.id), | |
status: MessageStore.getStatusForThread(thread.id) | |
}; | |
} | |
var MessageList = React.createClass({ | |
displayName: "messages", | |
render: function(){ | |
var status = this.state.status; | |
var content; | |
if(status === Constants.request.PENDING){ | |
content = (<Spinner />); | |
} else if (status === Constants.request.ERROR) { | |
content = "Daaaangerzone!"; | |
} else if(status === Constants.request.SUCCESS){ | |
content = this.state.messages.map(function(message){ | |
return ( | |
<div key={message.id}> | |
{message.user}: <i>{message.text}</i> | |
</div> | |
) | |
}).toJS(); | |
} | |
return ( | |
<div> | |
{content} | |
</div> | |
) | |
}, | |
getInitialState: function(){ | |
return getState(); | |
}, | |
componentDidMount: function() { | |
MessageStore.addChangeListener(this._onChange); | |
ThreadStore.addChangeListener(this._onChange); | |
}, | |
componentWillUnmount: function() { | |
MessageStore.removeChangeListener(this._onChange); | |
ThreadStore.removeChangeListener(this._onChange); | |
}, | |
_onChange: function(){ | |
this.setState(getState()); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_messages = []; | |
var MessageStore = assign({},EventEmitter.prototype, { | |
getAllForGroup: function (groupID) { | |
var status = this.getStatusForGroup(groupID); | |
if(!status){ | |
MessageActions.loadMessages(groupID); | |
return; | |
} | |
var cached = _messages.filter(function(v,k){ | |
return (v.group === groupID) | |
}); | |
return cached; | |
}, | |
dispatcherIndex: AppDispatcher.register(function (payload) { | |
var action = payload.action; | |
var groupId = action.groupId; | |
switch (action.actionType) { | |
case Constants.messages.LOAD: | |
_groups = _groups.set(groupId, Constants.request.PENDING); | |
break; | |
case Constants.messages.LOAD_SUCCESS: | |
_handleGetMessage(action); | |
break; | |
case Constants.messages.LOAD_ERROR: | |
_groups = _groups.set(groupId, Constants.request.ERROR); | |
break; | |
default: | |
return true; | |
} | |
MessageStore.emitChange(); | |
return true; | |
}), | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _selectedThread = null; | |
var _threads = []; | |
var ThreadStore = merge({},EventEmitter.prototype, { | |
getThreads: function(){ | |
return _threads; | |
}, | |
getSelectedThread: function(){ | |
if(_selectedGroup){ | |
return _threads[_selectedThread]; | |
} else { | |
return _threads[0]; | |
} | |
}, | |
dispatcherIndex: AppDispatcher.register(function(payload){ | |
var action = payload.action; | |
switch(action.actionType) { | |
case AppConstants.UserGroup.SELECT_GROUP: | |
_selectedGroup = action.id; | |
break; | |
default: | |
return true; | |
} | |
ThreadStore.emitChange(); | |
return true; | |
}), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment