Skip to content

Commit 2e9a599

Browse files
committed
docs: update rtdb example
1 parent 3a8ac26 commit 2e9a599

File tree

1 file changed

+50
-18
lines changed

1 file changed

+50
-18
lines changed

Diff for: examples/rtdb.html

+50-18
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
<meta charset="utf-8" />
55
<title>VueFire Todo App Demo</title>
66
<script src="https://www.gstatic.com/firebasejs/7.7.0/firebase.js"></script>
7-
<script src="https://unpkg.com/vue/dist/vue.js"></script>
8-
<script src="../dist/vuefire.js"></script>
7+
<script src="https://unpkg.com/vue@3"></script>
8+
<script src="../dist/vuefire.global.js"></script>
99
</head>
1010
<body>
1111
<!--
@@ -24,54 +24,86 @@
2424
-->
2525

2626
<div id="app">
27-
<input v-model.trim="newTodoText" @keyup.enter="addTodo" placeholder="Add new todo" />
27+
<input
28+
v-model.trim="newTodoText"
29+
@keyup.enter="addTodo"
30+
placeholder="Add new todo"
31+
/>
2832
<ul>
2933
<li v-for="todo in todos">
30-
<input :value="todo.text" @input="updateTodoText(todo, $event.target.value)" />
34+
<input
35+
:value="todo.text"
36+
@input="updateTodoText(todo, $event.target.value)"
37+
/>
3138
<button @click="removeTodo(todo)">X</button>
3239
</li>
3340
</ul>
3441
</div>
3542

3643
<script>
37-
/* global Vue, firebase */
38-
Vue.use(Vuefire.rtdbPlugin, { wait: true })
3944
var db = firebase
4045
.initializeApp({
4146
databaseURL: 'https://vuefiredemo.firebaseio.com',
4247
})
4348
.database()
4449
var todosRef = db.ref('todos')
4550

46-
new Vue({
47-
el: '#app',
48-
data: {
51+
const OptionsAPI = {
52+
data: () => ({
4953
newTodoText: '',
5054
todos: [],
51-
},
55+
}),
5256
firebase: {
5357
todos: todosRef.limitToLast(25),
5458
},
5559
methods: {
56-
addTodo: function() {
60+
addTodo: function () {
5761
if (this.newTodoText) {
5862
todosRef.push({
5963
text: this.newTodoText,
6064
})
6165
this.newTodoText = ''
6266
}
6367
},
64-
updateTodoText: function(todo, newText) {
65-
todosRef
66-
.child(todo['.key'])
67-
.child('text')
68-
.set(newText)
68+
updateTodoText: function (todo, newText) {
69+
todosRef.child(todo['.key']).child('text').set(newText)
6970
},
70-
removeTodo: function(todo) {
71+
removeTodo: function (todo) {
7172
todosRef.child(todo['.key']).remove()
7273
},
7374
},
74-
})
75+
}
76+
77+
const CompositionAPI = {
78+
setup() {
79+
const newTodoText = Vue.ref('')
80+
const todos = Vue.ref([])
81+
82+
Vuefire.rtdbBind(todos, todosRef.limitToLast(25))
83+
84+
function addTodo() {
85+
if (newTodoText.value) {
86+
todosRef.push({
87+
text: newTodoText.value,
88+
})
89+
newTodoText.value = ''
90+
}
91+
}
92+
function updateTodoText(todo, newText) {
93+
todosRef.child(todo['.key']).child('text').set(newText)
94+
}
95+
96+
function removeTodo(todo) {
97+
todosRef.child(todo['.key']).remove()
98+
}
99+
100+
return { newTodoText, todos, addTodo, updateTodoText, removeTodo }
101+
},
102+
}
103+
104+
let app = Vue.createApp(CompositionAPI)
105+
.use(Vuefire.rtdbPlugin, { wait: true })
106+
.mount('#app')
75107
</script>
76108
</body>
77109
</html>

0 commit comments

Comments
 (0)