Skip to content

Commit 4cdd81a

Browse files
committed
vuejs computed properties
1 parent 6fdae01 commit 4cdd81a

File tree

5 files changed

+140
-0
lines changed

5 files changed

+140
-0
lines changed

vuejs-computed-properties/App.vue

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<template>
2+
<div>
3+
<button @click="flipSort">Flip Sort</button>
4+
<ul>
5+
<li v-for="bear in bears" :key="bear.id">
6+
{{bear.name}}
7+
</li>
8+
</ul>
9+
<button @click="newBear">New Bear</button>
10+
</div>
11+
</template>
12+
13+
<script>
14+
module.exports = {
15+
props: ['beardb'],
16+
data() {
17+
return {
18+
sort: 1,
19+
rawBears: this.beardb
20+
}
21+
},
22+
computed: {
23+
bears: {
24+
get() {
25+
console.log('HELLO!')
26+
const result = Object.keys(this.rawBears).map((id) => {
27+
const bear = this.rawBears[id]
28+
bear.id = id
29+
return bear
30+
})
31+
result.sort((a, b) => {
32+
return a.name.localeCompare(b.name) * this.sort
33+
})
34+
return result
35+
},
36+
set(newValue) {
37+
this.$set(this.rawBears, 'autoid', {
38+
name: newValue
39+
})
40+
}
41+
}
42+
},
43+
watch: {
44+
bears(newValue, oldValue) {
45+
46+
}
47+
},
48+
methods: {
49+
flipSort() {
50+
this.sort = this.sort === 1 ? -1 : 1
51+
},
52+
newBear() {
53+
this.bears = 'Ozma'
54+
// this.$set(this.rawBears, 'newid', {
55+
// name: 'Steve'
56+
// })
57+
// this.rawBears = Object.assign({}, this.rawBears, {
58+
// 'newid': {
59+
// name: 'Steve'
60+
// }
61+
// })
62+
// this.beardb['newid'] = {
63+
// name: 'Steve'
64+
// }
65+
// delete this.beardb['bear1']
66+
}
67+
}
68+
}
69+
</script>

vuejs-computed-properties/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Vue.js Computed Properties
2+
3+
> [https://www.youtube.com/watch?v=8antoF7LyIo](https://www.youtube.com/watch?v=8antoF7LyIo)
4+
5+
Install [Node.js](https://nodejs.org/).
6+
7+
Within this folder run the terminal command `npm install` to install the
8+
`dependencies` and `devDependencies`.
9+
10+
Then run `npm start` to run the app viewable on `http://localhost:9966`.

vuejs-computed-properties/index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Document</title>
6+
</head>
7+
<body>
8+
<div id="app"></div>
9+
<script src="bundle.js" charset="utf-8"></script>
10+
</body>
11+
</html>

vuejs-computed-properties/index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
const Vue = require('vue')
2+
const App = require('./App.vue')
3+
4+
const beardb = {
5+
'bear1': {
6+
name: 'Oliver',
7+
type: 'grizzly'
8+
},
9+
'bear3': {
10+
name: 'Sheryl',
11+
type: 'brown'
12+
},
13+
'bear55': {
14+
name: 'Frank',
15+
type: 'polar'
16+
},
17+
}
18+
19+
new Vue({
20+
el: '#app',
21+
render: function (h) {
22+
return h(App, {
23+
props: {
24+
beardb: beardb
25+
}
26+
})
27+
}
28+
})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "vuejs-computed-properties",
3+
"version": "0.1.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "budo index.js:bundle.js -- -t vueify",
8+
"test": "node test.js"
9+
},
10+
"browser": {
11+
"vue": "vue/dist/vue.common.js"
12+
},
13+
"author": "Kyle Robinson Young <kyle@dontkry.com> (http://dontkry.com)",
14+
"license": "MIT",
15+
"devDependencies": {
16+
"budo": "^11.2.0",
17+
"vueify": "^9.4.1"
18+
},
19+
"dependencies": {
20+
"vue.js": "^0.3.2"
21+
}
22+
}

0 commit comments

Comments
 (0)