Skip to content

Commit cf5fdf4

Browse files
committed
Add a simple form binding example
1 parent 25cd054 commit cf5fdf4

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

docs/common-patterns.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,68 @@ export default new Vuex.Store({
465465
})
466466
```
467467

468+
## Form Binding
469+
470+
Use the Model classes to reduce the boilerplate required to work with forms and Vuex, even in strict mode! Every model instance has a `.clone()` method which can be used to get a fully-reactive copy of the record in the store. Here is a very simple version of how you could bind to a form and submit new data to the server.
471+
472+
```vue
473+
<template>
474+
<div class="bg-white h-full p-6">
475+
<h1>Create Todo</h1>
476+
477+
<form @submit.prevent="createTodo">
478+
<input v-model="clone.name" type="text" class="form-input" />
479+
<button
480+
type="submit"
481+
class="bg-blue-500 px-4 py-2 rounded text-white ml-2"
482+
>
483+
Create Todo
484+
</button>
485+
</form>
486+
</div>
487+
</template>
488+
489+
<script>
490+
export default {
491+
name: 'Todos',
492+
mixins: [makeFindMixin({ service: 'todos', watch: true })],
493+
data: () => ({
494+
todo: null,
495+
clone: null
496+
}),
497+
computed: {
498+
todosParams() {
499+
return {
500+
query: {},
501+
paginate: false
502+
}
503+
}
504+
},
505+
created() {
506+
const { Todo } = this.$FeathersVuex.myApi
507+
this.todo = new Todo({})
508+
this.clone = this.todo.clone()
509+
},
510+
methods: {
511+
async createTodo() {
512+
try {
513+
const todo = await this.clone.save()
514+
console.log(todo)
515+
} catch (error) {
516+
console.log(error)
517+
}
518+
},
519+
updateTodo(ev, todo) {
520+
todo.isComplete = ev.target.checked
521+
todo.save()
522+
}
523+
}
524+
}
525+
</script>
526+
527+
<style lang="postcss"></style>
528+
```
529+
468530
## Multiple Copies
469531

470532
The previous version of `feathers-vuex` was hard-coded to allow for a single `current` record and one copy. It was pretty easy to hit that limit. This new release allows for keeping many more copies, one copy per stored record. To make it easier to comply with Vuex's `strict` mode, copies are not kept in the store by default, but are instead kept on `Model.copiesById`. You can make changes to the copies without having to make custom mutations, then you can commit them back into the store:

0 commit comments

Comments
 (0)