This repository has been archived by the owner on Apr 19, 2019. It is now read-only.
/
index.html
97 lines (88 loc) · 2.43 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>VuexFire Todo App Demo</title>
<script src="https://www.gstatic.com/firebasejs/3.7.5/firebase.js"></script>
<script src="https://unpkg.com/vue/dist/vue.js"></script>
<script src="https://unpkg.com/vuex/dist/vuex.js"></script>
<script src="../../dist/vuexfire.js"></script>
</head>
<body>
<!--
To see a live example, head to
https://jsfiddle.net/posva/6w3ks04x/
-->
<div id="app">
<ul>
<li v-for="item in items" :key="item['.key']">
{{ item.text }}
<button @click="removeTodo(item['.key'])">X</button>
</li>
</ul>
<form @submit.prevent="addTodo">
<input v-model="newTodo">
<button>Add #{{ items.length }}</button>
</form>
<br/>
<br/>
<button @click="toggleSource">Toggle Source</button>
</div>
<script>
/* global Vue, Vuex, firebase, VuexFire */
var config = {
databaseURL: 'https://vuexfire.firebaseio.com',
}
var firebaseApp = firebase.initializeApp(config)
var db = firebaseApp.database()
var itemsRef = db.ref('test')
var todosRef = db.ref('test_2')
var store = new Vuex.Store({
// VuexFire will check the type of the property to bind as an array or as
// an object
strict: true,
state: { items: [] },
mutations: VuexFire.firebaseMutations,
getters: {
items: state => state.items,
},
actions: {
setItemsRef: VuexFire.firebaseAction(({ bindFirebaseRef }, ref) => {
bindFirebaseRef('items', ref, { wait: true })
}),
},
})
window.vm = new Vue({
el: '#app',
store: store,
computed: Vuex.mapGetters([ 'items' ]),
data: {
newTodo: '',
},
methods: {
removeTodo (key) {
this.source.child(key).remove()
},
addTodo () {
if (this.newTodo.trim()) {
this.source.push({
text: this.newTodo,
})
this.newTodo = ''
}
},
toggleSource () {
this.source = this.source === itemsRef
? todosRef
: itemsRef
this.$store.dispatch('setItemsRef', this.source)
},
},
created () {
this.source = itemsRef
this.$store.dispatch('setItemsRef', this.source)
},
})
</script>
</body>
</html>