/
index.html
116 lines (102 loc) 路 3.16 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
104
105
106
107
108
109
110
111
112
113
114
115
116
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>VueFire Todo App Demo</title>
<script src="https://www.gstatic.com/firebasejs/8.1.2/firebase.js"></script>
<script src="https://unpkg.com/vue@2"></script>
<script src="../dist/vuefire.js"></script>
</head>
<body>
<!--
Before running this example, make sure to:
1. cd path/to/vuefire
2. npm install
3. npm run build
Then you can open this file in your browser.
If you just prefer to see this example with
the latest published version of VueFire, you
play with the code in this fiddle:
https://jsfiddle.net/posva/wtyop5jy/
-->
<div id="app">
<button @click="toggleTodos">Toggle todos</button> <br />
<input v-model.trim="newTodoText" @keyup.enter="addTodo" placeholder="Add new todo" />
<ul>
<li v-for="todo in todos">
<input :value="todo.text" @input="updateTodoText(todo, $event.target.value)" />
<button @click="removeTodo(todo)">X</button>
</li>
</ul>
<h4>collection with refs</h4>
<ul>
<li v-for="moment in moments">{{ moment }}</li>
</ul>
<h5>Original data</h5>
<ul>
<li v-for="tweet in tweets">{{ tweet }}</li>
</ul>
<p>config:</p>
<pre>
{{ config }}
</pre>
</div>
<script>
/* global Vue, firebase, Vuefire */
Vue.use(Vuefire.firestorePlugin, { wait: true })
var db = firebase
.initializeApp({
projectId: 'vue-fire-store',
databaseURL: 'https://vue-fire-store.firebaseio.com',
})
.firestore()
var todos = db.collection('todos')
var unFinishedTodos = todos.where('finished', '==', false)
var finishedTodos = todos.where('finished', '==', true)
var config = db.collection('configs').doc('jORwjIykFo2NmkdzTkhU')
var vm = new Vue({
el: '#app',
data: {
todos: [],
tweets: [],
moments: [],
nested: [],
config: null,
newTodoText: '',
},
firestore: {
todos: unFinishedTodos,
moments: db.collection('moments'),
nested: db.collection('nested'),
tweets: db.collection('tweets'),
config: config,
},
methods: {
addTodo: function() {
if (this.newTodoText) {
todos.add({
finished: false,
text: this.newTodoText,
created: firebase.firestore.FieldValue.serverTimestamp(),
})
this.newTodoText = ''
}
},
updateTodoText: function(todo, newText) {
todos.doc(todo.id).update({ text: newText })
},
removeTodo: function(todo) {
todos.doc(todo.id).delete()
},
toggleTodos: function() {
console.log(this.$firestoreRefs.todos === unFinishedTodos)
this.$bind(
'todos',
this.$firestoreRefs.todos === unFinishedTodos ? finishedTodos : unFinishedTodos
)
},
},
})
</script>
</body>
</html>