Skip to content

Instantly share code, notes, and snippets.

@suchitpuri
Created September 24, 2015 10:32
Show Gist options
  • Save suchitpuri/343562bd486dc70d2f35 to your computer and use it in GitHub Desktop.
Save suchitpuri/343562bd486dc70d2f35 to your computer and use it in GitHub Desktop.
The store for managing the page loading
export default {
START: "startLoading",
DONE: "loadingComplete",
START_BUTTON: "startButtonLoading",
DONE_BUTTON: "doneButtonLoading"
}
import ActionTypes from './action-types.js';
import {handleAction} from '../../dispatcher';
// import HelloSlave from '../slaves/HelloSlave.js';
export default {
startPageLoading: function() {
handleAction({
actionType: ActionTypes.START
});
},
startButtonLoading: function() {
handleAction({
actionType: ActionTypes.START_BUTTON
});
},
doneButtonLoading: function() {
handleAction({
actionType: ActionTypes.DONE_BUTTON
});
},
donePageLoading: function() {
handleAction({
actionType: ActionTypes.DONE
});
}
};
// import AppDispatcher from '../app-dispatcher';
import ActionTypes from './action-types.js';
import {EventEmitter} from 'events';
import {register} from '../../dispatcher';
let _data = {
pageLoader: false,
butonLoader: false
};
function _startPageLoader() {
_data.pageLoader = 30;
}
function _startButtonLoader(){
_data.butonLoader = true;
}
function _completeButtonLoader(){
_data.butonLoader = false;
}
function _completePageLoader(){
_data.pageLoader = 100;
setTimeout(function(){
_data.pageLoader = false;
_LoaderStore.emitChange();
},400)
}
class LoaderStore extends EventEmitter {
getPageLoader() {
return _data.pageLoader;
}
getButtonLoader(){
return _data.butonLoader;
}
emitChange() {
this.emit('change');
}
addChangeListener(callback) {
this.on('change', callback);
}
removeChangeListener(callback) {
this.removeListener('change', callback);
}
}
let _LoaderStore = new LoaderStore();
export default _LoaderStore;
register(({action, data}) => {
switch (action.actionType) {
case ActionTypes.START:
_startPageLoader();
_LoaderStore.emitChange();
break;
case ActionTypes.DONE:
_completePageLoader();
_LoaderStore.emitChange();
break;
case ActionTypes.START_BUTTON:
_startButtonLoader();
_LoaderStore.emitChange();
break;
case ActionTypes.DONE_BUTTON:
_completeButtonLoader();
_LoaderStore.emitChange();
break;
default:
break;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment