This repository has been archived by the owner on Apr 19, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 53
/
index.html
103 lines (90 loc) · 2.5 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
98
99
100
101
102
103
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>VuexFire Todo App Demo</title>
<script src="https://www.gstatic.com/firebasejs/4.13.0/firebase.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.13.0/firebase-firestore.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.id">
{{ item.text }}
<button @click="removeTodo(item.id)">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 */
firebase.initializeApp({
projectId: 'vue-fire-store',
databaseURL: 'https://vue-fire-store.firebaseio.com'
})
var db = firebase.firestore()
db.settings({ timestampsInSnapshots: true })
var itemsRef = db.collection('vuexfireItems1')
var todosRef = db.collection('vuexfireItems2')
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)
}),
},
})
window.vm = new Vue({
el: '#app',
store: store,
computed: Vuex.mapGetters(['items']),
data: {
newTodo: '',
},
methods: {
removeTodo(id) {
this.source.doc(id).delete()
},
addTodo() {
if (this.newTodo.trim()) {
this.source.add({
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>