Created
February 12, 2015 21:03
-
-
Save goatslacker/06709de487e14d845747 to your computer and use it in GitHub Desktop.
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 Alt = require('alt') | |
var alt = new Alt() | |
function UserActions() { | |
this.generateActions('getUser', 'addUser') | |
} | |
var ua = alt.createActions(UserActions) | |
function UserStore() { | |
this.bindActions(ua) | |
this.currentUser = BlankLoadingUserTemplate() | |
} | |
UserStore.prototype.onGetUser = function (id) { | |
if (this.users[id]) { | |
this.currentUser = this.users[id] | |
} else { | |
UserUtils.findUser(id) | |
return false | |
} | |
} | |
UserStore.prototype.onAddUser = function (userObj) { | |
this.users[userObj.id] = userObj | |
this.currentUser = userObj | |
} | |
var us = alt.createStore(UserStore) | |
var UserUtils = { | |
findUser: function (id) { | |
fetch('/users', { id: id }, function (res) { | |
ua.addUser(res.body) | |
}) | |
} | |
} | |
// view | |
ua.getUser() | |
us.listen(function () { | |
console.log(us.getState().currentUser) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment