0.2.33 • Published 5 years ago

vue-eloquent v0.2.33

Weekly downloads
1
License
ISC
Repository
gitlab
Last release
5 years ago

See the Docs page.

A Vue plugin to make your code yet again a little more readable.

Install the plugin

Install the plugin in your main javascript file: main.js

import Vue from 'vue';

import VueEloquent from 'vue-eloquent';
Vue.use(VueEloquent);

Create a collection

In a Vue component we can now create a "smart" collection with this.$collection. After this we are able to fetch the collection from the server with this.todos.fetch(). Todos.vue

<template>
	<div v-for="todo in todos" :key="todo.id">
		{{ todo.description }}
	</div>
</template>

<script>
	export default {
		mounted() {
			this.todos.fetch();
		},
		data() {
			return {
				todos: this.$collection({
					url: '/api/todos' // I need this to know where the api calls should go.
				})
			}
		}
	}
</script>

Update and remove $models

Because a $collection is filled with super smart $models we can now do stuff like this: Todos.vue

<template>
	<div>
		<div v-for="todo in todos" :key="todo.id">
			<span @click="todo.update({ status: !todo.status })">
				{{ todo.status ? 'O' : 'X' }}
			</span>
			<span>{{ todo.description }}</span>
			<button @click.stop="todo.remove">X</button>
		</div>
	</div>
</template>

<script>
	export default {
		mounted() {
			this.todos.fetch();
		},
		data() {
			return {
				todos: this.$collection({
					url: '/api/todos'
				})
			}
		}
	}
</script>

In the above code todo.update will make an axios.put call with the todo as payload. Remove will make an axios.delete call width the id.

Create a new model

Todos.vue

<template>
	<div>
		<input
			type="text"
			placeholder="New todo"
			@keyup.enter="createTodo"
			v-model="newTodo" />
		<div v-for="todo in todos" :key="todo.id">
			<span @click="todo.update({ status: !todo.status })">
				{{ todo.status ? 'O' : 'X' }}
			</span>
			<span>{{ todo.description }}</span>
			<button @click.stop="todo.remove">X</button>
		</div>
	</div>
</template>

<script>
	export default {
		mounted() {
			this.todos.fetch();
		},
		methods: {
			createTodo() {
				this.todos
					.create({ description: this.newTodo })
					.then(resp => {
						this.newTodo = '';
					});
			}
		},
		data() {
			return {
				todos: this.$collection({
					url: '/api/todos'
				}),
				newTodo: ''
			}
		}
	}
</script>
0.2.33

5 years ago

0.2.32

5 years ago

0.2.31

5 years ago

0.2.30

5 years ago

0.2.29

5 years ago

0.2.28

5 years ago

0.2.27

5 years ago

0.2.26

5 years ago

0.2.25

5 years ago

0.2.24

5 years ago

0.2.23

5 years ago

0.2.22

5 years ago

0.2.21

5 years ago

0.2.19

5 years ago

0.2.18

6 years ago

0.2.17

6 years ago

0.2.16

6 years ago

0.2.15

6 years ago

0.2.14

6 years ago

0.2.13

6 years ago

0.2.12

6 years ago

0.2.11

6 years ago

0.2.10

6 years ago

0.2.9

6 years ago

0.2.8

6 years ago

0.2.7

6 years ago

0.2.6

6 years ago

0.2.5

6 years ago

0.2.4

6 years ago

0.2.3

6 years ago

0.2.2

6 years ago

0.2.1

6 years ago

0.2.0

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.8

6 years ago

0.0.7

6 years ago

0.0.6

6 years ago

0.0.5

6 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago