Smart stores for Riot (like flux, based on ideas from RiotControl)
Small (<1.5Kb) flux-like store container.
Make sure loot.js is included in your page after riot.js:
<script src="riot.js"></script>
<script src="loot.js"></script>
Then you can create your stores:
stores.js
riot.loot.register('auth', function controller(store){
store.isLoggedIn = false
store.message = null
store.onAction({
authenticate: function(username, password) {
if (username === 'bob' && password === 'smith' ) {
store.isLoggedIn = true
store.message = "Welcome, Bob!"
}
else {
store.isLoggedIn = false
store.message = "Username or password invalid, please try again."
}
// return false if you DON'T want to trigger a 'change' event.
},
logout: function() {
store.isLoggedIn = false
store.message = "You've been logged out."
}
})
})
login-page.tag
<login-page>
<form onsubmit={login}>
<div if={auth.message}>
<div class="message">{ auth.message }</div>
</div>
<div>
<input name="username" placeholder="Username">
</div>
<div>
<input name="password" placeholder="Password" type="password">
</div>
<div>
<button disabled={auth.isLoggedIn || !username.value || !password.value}>
Login
</button>
<button disabled={!auth.isLoggedIn} onclick={logout} type="button">
Logout
</button>
</div>
</form>
<script>
this.mixin('loot')
// this.getStore( storeName ) -- provided by the mixin -- will setup
// this component to automatically call this.update() whenever the store
// triggers a 'change' event.
this.auth = this.getStore('auth')
// This event handler will be cleaned up when the component unmounts.
this.auth.on('success', () => {
// on auth store event 'success'
})
login(e) {
this.sendAction(
'authenticate',
this.username.value,
this.password.value
)
this.password.value = ''
}
logout(e) {
this.sendAction('logout')
}
</script>
<style>
.message {
background-color: dodgerblue;
color: white;
}
</style>
</login-page>