Skip to content

Commit 15bee32

Browse files
committed
add firebase example
1 parent 2c13ecb commit 15bee32

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

examples/firebase/app.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
var baseURL = 'https://vue-demo.firebaseIO.com/'
2+
var emailRE = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
3+
4+
/**
5+
* Setup firebase sync
6+
*/
7+
8+
var Users = new Firebase(baseURL + 'users')
9+
10+
Users.on('child_added', function (snapshot) {
11+
var item = snapshot.val()
12+
item.id = snapshot.name()
13+
app.users.push(item)
14+
})
15+
16+
Users.on('child_removed', function (snapshot) {
17+
var id = snapshot.name()
18+
app.users.some(function (user) {
19+
if (user.id === id) {
20+
app.users.$remove(user)
21+
return true
22+
}
23+
})
24+
})
25+
26+
/**
27+
* Create Vue app
28+
*/
29+
30+
var app = new Vue({
31+
32+
// element to mount to
33+
el: '#app',
34+
35+
// initial data
36+
data: {
37+
users: [],
38+
newUser: {
39+
name: '',
40+
email: ''
41+
},
42+
validation: {
43+
name: false,
44+
email: false
45+
}
46+
},
47+
48+
// validation filters are "write only" filters
49+
filters: {
50+
nameValidator: {
51+
write: function (val) {
52+
this.validation.name = !!val
53+
return val
54+
}
55+
},
56+
emailValidator: {
57+
write: function (val) {
58+
this.validation.email = emailRE.test(val)
59+
return val
60+
}
61+
}
62+
},
63+
64+
// computed property for form validation state
65+
computed: {
66+
isValid: function () {
67+
var valid = true
68+
for (var key in this.validation) {
69+
if (!this.validation[key]) {
70+
valid = false
71+
}
72+
}
73+
return valid
74+
}
75+
},
76+
77+
// methods
78+
methods: {
79+
addUser: function (e) {
80+
e.preventDefault()
81+
if (this.isValid) {
82+
Users.push(this.newUser)
83+
this.newUser = {
84+
name: '',
85+
email: ''
86+
}
87+
}
88+
},
89+
removeUser: function (user) {
90+
new Firebase(baseURL + 'users/' + user.id).remove()
91+
}
92+
}
93+
})

examples/firebase/index.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title></title>
5+
<meta charset="utf-8">
6+
<link rel="stylesheet" type="text/css" href="style.css">
7+
<script src='https://cdn.firebase.com/js/client/1.0.23/firebase.js'></script>
8+
<script src="../../dist/vue.js"></script>
9+
</head>
10+
<body>
11+
<div id="app">
12+
<ul>
13+
<li class="user" v-repeat="users" v-transition>
14+
<span>{{name}} - {{email}}</span>
15+
<button v-on="click:removeUser(this)">X</button>
16+
</li>
17+
</ul>
18+
<form id="form" v-on="submit:addUser">
19+
<input v-model="newUser.name | nameValidator | capitalize">
20+
<input v-model="newUser.email | emailValidator">
21+
<input type="submit" value="Add User">
22+
</form>
23+
<ul class="errors">
24+
<li v-show="!validation.name">Name cannot be empty.</li>
25+
<li v-show="!validation.email">Please provide a valid email address.</li>
26+
</ul>
27+
</div>
28+
<script src="app.js"></script>
29+
</body>
30+
</html>

examples/firebase/style.css

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
ul {
2+
padding: 0;
3+
}
4+
5+
.user {
6+
height: 30px;
7+
line-height: 30px;
8+
padding: 10px;
9+
border-top: 1px solid #eee;
10+
overflow: hidden;
11+
transition: all .25s ease;
12+
}
13+
14+
.user:last-child {
15+
border-bottom: 1px solid #eee;
16+
}
17+
18+
.v-enter, .v-leave {
19+
height: 0;
20+
padding-top: 0;
21+
padding-bottom: 0;
22+
border-top-width: 0;
23+
border-bottom-width: 0;
24+
}
25+
26+
.errors {
27+
color: #f00;
28+
}

0 commit comments

Comments
 (0)