Skip to content

Instantly share code, notes, and snippets.

@JedWatson
Created March 26, 2015 00:40
Show Gist options
  • Save JedWatson/18eb2582f5d957053a1d to your computer and use it in GitHub Desktop.
Save JedWatson/18eb2582f5d957053a1d to your computer and use it in GitHub Desktop.
Simple store-prototype example for a User Store + View
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>;
}
});
@lisat1312
Copy link

lisat1312 commented Dec 9, 2024

To create a simple store-prototype for a User Store + View, follow these steps:

  1. Set Up a User Store:
    Use a basic JavaScript object or a library like Redux for managing the user state. Example:

    const userStore = {
        users: [],
        addUser(user) {
            this.users.push(user);
        },
        getUsers() {
            return this.users;
        },
    };
  2. Create a View:
    Use HTML and JavaScript to display the user data.

    <div id="user-list"></div>
    <button onclick="addNewUser()">Add User</button>
    <script>
        function addNewUser() {
            const user = { name: 'John Doe', id: userStore.users.length + 1 };
            userStore.addUser(user);
            renderUsers();
        }
    
        function renderUsers() {
            const userList = document.getElementById('user-list');
            userList.innerHTML = userStore.getUsers()
                .map(user => `<p>${user.id}: ${user.name}</p>`)
                .join('');
        }
    </script>
  3. Integrate:
    Combine the store and view for dynamic updates when the store changes.

For a detailed implementation, www.goodwilloutlets.net.

@lucasaaa49611
Copy link

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());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment