-
-
Save JedWatson/18eb2582f5d957053a1d to your computer and use it in GitHub Desktop.
var Store = require('store-prototype'); | |
var UserStore = new Store(); | |
var users = {}; | |
var loaded = false; | |
UserStore.extend({ | |
loadUsers: function() { | |
loadUsersSomehow(function(err, results) { | |
users = results; | |
this.notifyChange(); | |
}); | |
}, | |
getUsers: function() { | |
if (!loaded) this.loadUsers(); | |
return users; | |
}, | |
getUser: function(id) { | |
return users[id]; | |
}, | |
isLoaded: function() { | |
return loaded; | |
}, | |
updateUser: function(data) { | |
updateUserSomehow(data, function(err, result) { | |
users[result.id] = result; | |
this.notifyChange(); | |
}); | |
}, | |
removeUser: function(id) { | |
delete users[id]; | |
this.notifyChange(); | |
removeUserSomehow(id, function(err) { | |
// rollback on err maybe? | |
}); | |
} | |
}); | |
module.exports = UserStore; |
var UserStore = require('UserStore'); | |
var UserView = React.createClass({ | |
getInitialState: function() { | |
return this.getUserState(); | |
}, | |
getUserState: function() { | |
return { | |
users: UserStore.getUsers(), | |
ready: UserStore.isLoaded() | |
}; | |
}, | |
updateUserState: function() { | |
this.setState(this.getUserState()); | |
}, | |
componentDidMount: function() { | |
UserStore.addChangeListener(this.updateUserState); | |
if (!this.state.ready) UserStore.loadUsers(); | |
}, | |
componentWillUnmount: function() { | |
UserStore.removeChangeListener(this.updateUserState); | |
}, | |
render: function() { | |
if (!this.ready) return <div>Loading...</div>; | |
return <UserList users={this.state.users} />; | |
} | |
}); | |
var UserList = React.createComponent({ | |
render: function() { | |
var rows = this.props.users.map(function(user) { | |
return <UserRow user={user} /> | |
}); | |
return <div>{rows}</div>; | |
} | |
}); | |
var UserRow = React.createComponent({ | |
removeUser: function() { | |
UserStore.removeUser(this.props.user.id); | |
}, | |
render: function() { | |
return ( | |
<div> | |
{this.props.user.name} | |
<button onclick={this.removeUser}>remove</button> | |
</div>; | |
} | |
}); |
If you're exploring prototypes for a user store and view, you might also be interested in sustainable shopping solutions. Goodwill Outlets (https://goodwilloutlets.org/) provide an excellent way to find affordable, second-hand items while supporting a great cause. Just like efficient code structures improve user experience, mindful shopping at thrift stores promotes sustainability and helps communities. Have you ever considered incorporating thrift shopping into your lifestyle?
Here’s a simple prototype example for a User Store + View using JavaScript with a basic structure:
class UserStore {
constructor() {
this.users = [];
}
addUser(user) {
this.users.push(user);
}
getUser(name) {
return this.users.find(user => user.name === name);
}
getAllUsers() {
return this.users;
}
}
// Example Usage
const store = new UserStore();
store.addUser({ name: "Alice", age: 25 });
store.addUser({ name: "Bob", age: 30 });
console.log(store.getUser("Alice"));
console.log(store.getAllUsers());
To create a simple store-prototype for a User Store + View, follow these steps:
Set Up a User Store:
Use a basic JavaScript object or a library like Redux for managing the user state. Example:
Create a View:
Use HTML and JavaScript to display the user data.
Integrate:
Combine the store and view for dynamic updates when the store changes.
For a detailed implementation, www.goodwilloutlets.net.