Created
October 1, 2014 15:40
-
-
Save bloodyowl/f22950c3bd4dda5c8357 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 merge = require("react/lib/merge") | |
var Store = require("../utils/store") | |
var AppDispatcher = require("../dispatcher") | |
var AppConstants = require("../constants") | |
var ActionTypes = AppConstants.ActionTypes | |
var API = require("../api") | |
var _store = { | |
value : null, | |
pending : true | |
} | |
var MyStore = merge(Store, { | |
getCurrent : function(){ | |
return _store | |
}, | |
dispatchToken : AppDispatcher.register(function(payload){ | |
var action = payload.action | |
switch(action.type) { | |
case ActionTypes.STORENAME_API_RESPONDED: | |
_store.value = action.response | |
_store.pending = false | |
MyStore.emitChange() | |
break | |
case ActionTypes.STORENAME_FETCH | |
API.fetchFoo(action.name, action.params) | |
_store.pending = true | |
MyStore.emitChange() | |
break | |
} | |
}) | |
}) | |
module.exports = MyStore |
This file contains hidden or 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 EventEmitter = require("events").EventEmitter | |
var merge = require("react/lib/merge") | |
var CHANGE_EVENT = "change" | |
var Store = merge(EventEmitter.prototype, { | |
emitChange : function(){ | |
this.emit(CHANGE_EVENT) | |
}, | |
addChangeListener: function(callback){ | |
this.on(CHANGE_EVENT, callback) | |
}, | |
removeChangeListener: function(callback){ | |
this.removeListener(CHANGE_EVENT, callback) | |
}, | |
dispatchToken : null | |
}) | |
module.exports = Store |
This file contains hidden or 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 merge = require("react/lib/merge") | |
module.exports = function StoreMixin(Store, specifics){ | |
var defaults = { | |
getInitialState : function(){ | |
return Store.getCurrent() | |
}, | |
_onChange : function(){ | |
this.setState(Store.getCurrent()) | |
}, | |
componentDidMount : function(){ | |
Store.addChangeListener(this._onChange) | |
}, | |
componentWillUnmount : function(){ | |
Store.removeChangeListener(this._onChange) | |
} | |
} | |
if(specifics) { | |
return merge(defaults, specifics) | |
} | |
return defaults | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment