-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from rtfeldman/integrate-client-server
WIP: Integrate client and server via Flux
- Loading branch information
Showing
7 changed files
with
152 additions
and
1 deletion.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
var Dispatcher = function() { | ||
this.handlers = []; | ||
this.isDispatching = false; | ||
this.pendingPayload = null; | ||
} | ||
|
||
Dispatcher.prototype.register = function(callback) { | ||
this.handlers.push({ | ||
isPending: false, | ||
isHandled: false, | ||
callback: callback | ||
}); | ||
}; | ||
|
||
Dispatcher.prototype.dispatch = function(payload) { | ||
if (this.isDispatching) { | ||
throw new Error("Cannot dispatch in the middle of a dispatch!"); | ||
} | ||
|
||
// Initialize states to begin the dispatch | ||
this.handlers.forEach(function(handler) { | ||
handler.isPending = false; | ||
handler.isHandled = false; | ||
}); | ||
|
||
this.pendingPayload = payload; | ||
this.isDispatching = true; | ||
|
||
try { | ||
// Invoke the handler callbacks | ||
this.handlers.forEach(function(handler) { | ||
if (!handler.isPending) { | ||
handler.isPending = true; | ||
handler.callback(payload); | ||
handler.isHandled = true; | ||
} | ||
}) | ||
} finally { | ||
// Clean up after the dispatch | ||
this.pendingPayload = null; | ||
this.isDispatching = false; | ||
} | ||
}; | ||
|
||
// The Dispatcher is a singleton, so export only the one instance. | ||
module.exports = new Dispatcher(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
var Dispatcher = require("./Dispatcher"); | ||
var SurveyConstants = require("./SurveyConstants"); | ||
|
||
var SurveyActions = { | ||
save: function(survey) { | ||
Dispatcher.dispatch({ | ||
actionType: SurveyConstants.SAVE_SURVEY, | ||
survey: survey | ||
}); | ||
}, | ||
|
||
delete: function(id) { | ||
Dispatcher.dispatch({ | ||
actionType: SurveyConstants.DELETE_SURVEY, | ||
id: id | ||
}); | ||
} | ||
} | ||
|
||
module.exports = SurveyActions; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
module.exports = { | ||
SAVE_SURVEY: "save", | ||
DELETE_SURVEY: "delete" | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
var EventEmitter = require("event-emitter"); | ||
var CHANGE_EVENT = "changeEvent"; | ||
|
||
var SurveyStore = function() { | ||
this.emitter = new EventEmitter(); | ||
}; | ||
|
||
// Basic event handling functions | ||
|
||
SurveyStore.prototype.emitChange = function() { | ||
this.emitter.emit(CHANGE_EVENT); | ||
}; | ||
|
||
SurveyStore.prototype.addChangeListener = function(callback) { | ||
this.emitter.on(CHANGE_EVENT, callback); | ||
}; | ||
|
||
SurveyStore.prototype.removeChangeListener = function(callback) { | ||
this.emitter.removeListener(CHANGE_EVENT, callback); | ||
}; | ||
|
||
|
||
|
||
// Survey-specific methods | ||
SurveyStore.prototype.saveSurvey = function(survey) { | ||
console.debug("TODO: fire XHR to persist survey, then invoke this.emitChange() after the XHR has completed."); | ||
|
||
this.emitChange(); | ||
} | ||
|
||
SurveyStore.prototype.deleteSurvey = function(id) { | ||
console.debug("TODO: delete survey", id); | ||
|
||
this.emitChange(); | ||
} | ||
|
||
SurveyStore.prototype.listSurveys = function(callback) { | ||
console.debug("TODO: fetch surveys from server via XHR"); | ||
|
||
callback([]); | ||
} | ||
|
||
// The SurveyStore is a singleton, so export only the one instance. | ||
module.exports = new SurveyStore(); |
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