Skip to content

Instantly share code, notes, and snippets.

@VitorLuizC
Last active November 6, 2017 16:30
Show Gist options
  • Save VitorLuizC/b5794cd7650e875ddc41eb288824709c to your computer and use it in GitHub Desktop.
Save VitorLuizC/b5794cd7650e875ddc41eb288824709c to your computer and use it in GitHub Desktop.
Shortly Vue
import compose from './compose'
export default (watchers, method) => {
const shorlty = compose(watchers,
(_, watcher) => ({
[watcher]: typeof method !== 'string' ? method : function () {
this[method](...arguments)
}
})
)
return shortly
}
/**
* @param {Object.<string, A>} object
* @param {function(string, A): Object.<string, B>} λ
* @template A, B
* @returns {Object.<string, B>}
*/
export default (object, λ) => {
const entries = Object.entries(object)
const composition = entries.reduce(
(composition, [ property, value ]) => ({
...composition,
...λ(property, value) || {}
}),
{}
)
return composition
}
<template>
<form>
<fieldset>
<label for="input-name">Name</label>
<input v-model="user.name" id="input-name" />
</fieldset>
<fieldset>
<label for="input-password">Password</label>
<input
v-model="user.password"
id="input-password"
type="password"
/>
</fieldset>
<blockquote v-if="error">
<p>{{ error }}</p>
</blockquote>
</form>
</template>
<script>
import shortlyWatchers from 'vue-shortly-watchers'
export default {
data () {
return {
error: '',
user: {
name: '',
password: ''
}
}
},
watch: shortlyWatchers(['user.name', 'user.password'], 'check'),
methods: {
check () {
this.error = (!this.user.name || !this.user.password)
? 'Empty name or password'
: ''
}
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment