Created
February 4, 2017 21:14
-
-
Save abiodun0/5a3e7bccf38c8313225b0a9c7bdb5d14 to your computer and use it in GitHub Desktop.
Some old ways of implementing flux architecture
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
import {EventEmitter} from 'events'; | |
import AppDispatcher from '../dispatcher/AppDispatcher'; | |
export default class BaseStore extends EventEmitter { | |
constructor() { | |
super(); | |
} | |
subscribe(actionSubscribe) { | |
this._dispatchToken = AppDispatcher.register(actionSubscribe()); | |
} | |
get dispatchToken() { | |
return this._dispatchToken; | |
} | |
emitChange() { | |
this.emit('CHANGE'); | |
} | |
addChangeListener(callback) { | |
this.on('CHANGE', callback); | |
} | |
removeChangeListener(callback) { | |
this.removeListener('CHANGE', callback); | |
} | |
} |
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
import AppDispatcher from '../dispatcher/AppDispatcher'; | |
class UserStore extends BaseStore { | |
constructor() { | |
super(); | |
this._users = {}; | |
this.subscribe(() => this._registerActions.bind(this)); | |
this._currentUser = {}; | |
} | |
update(id, updates) { | |
this._users[id] = assign({}, this._users[id], updates); | |
} | |
updateCurrentUser(user) { | |
user = (typeof user === 'object') ? JSON.stringify(user) : user; | |
$.cookie(CVar.this.currentUser, user || {}); | |
this.update(user.id, user); | |
} | |
getAllUsers() { | |
return this._users; | |
} | |
getUser(id) { | |
return this._users[id]; | |
} | |
getCurrentUser() { | |
return AuthStore.getCurrentUser(); | |
} | |
getFullName(user) { | |
return (user.first_name || '') + (user.last_name || ''); | |
} | |
_registerActions(action) { | |
switch (action.actionType) { | |
case ZhishiConstants.RECEIVE_USER: | |
if (AppDispatcher.isDispatching()) { | |
AppDispatcher.waitFor([AuthStore.dispatchToken]); | |
} | |
if (action.data) { | |
this.update(action.data.id, action.data); | |
this.emitChange(); | |
} | |
break; | |
default: | |
// Nothing for now | |
} | |
} | |
} | |
export default new UserStore(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment