Skip to content

Commit ae8ded8

Browse files
committed
Phase7-vue-router
1. Refactor index.html to use router-link 2. Update main.js per commits to add the router. The docs were light on this as most examples talked about how to tie the routes to the componenets to show. Turns out its optional and we can use the `$router` var that get injected into the Vue.
1 parent 2cc1c5c commit ae8ded8

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- [Phase 4 - Grails Todo Rest Setup](#phase-4---grails-todo-rest-setup)
1010
- [Phase 5 - Vue TodoMVC modifications for rest model](#phase-5---vue-todomvc-modifications-for-rest-model)
1111
- [Phase 6 - Use v-model axios rest wrapper. Add error checking](#phase-6---use-v-model-axios-rest-wrapper-add-error-checking)
12+
- [Phase 7 - vue-router](#phase-7---vue-router)
1213

1314
<!-- /MarkdownTOC -->
1415

@@ -165,6 +166,16 @@ Date: Sun, 07 May 2017 06:01:57 GMT
165166

166167
Try creating a todo with _xxx_ as the title or modifying an existing one.
167168

169+
## Phase 7 - vue-router
170+
[see this commit for changes](https://github.com/basejump/grails-vuejs-todomvc-example/commit/b657f26b2eb6a580171bb634795916990a5f1862)
171+
172+
[vue-router](https://router.vuejs.org/en/) provide route and url view mapping. We were taking the url and parsing it with `function onHashChange ()` and `window.addEventListener('hashchange', onHashChange)`. We have `<a>` links to change the url for the filters on [all,active,completed]. The event listener bascially took the url when it changed from `http://localhost:8090/#/` to `http://localhost:8090/#/completed` and parse off the 'completed' part which is used to then propogate a refilter by setting `this.visibility`. [vue-router](https://router.vuejs.org/en/) is the stadard way of dealing with what to do when the url changes. Should already be installed.
173+
174+
175+
1. Refactor index.html to use router-link
176+
177+
2. Update main.js per commits to add the router. The docs were light on this as most examples talked about how to tie the routes to the componenets to show. Turns out its optional and we can use the `$router` var that get injected into the Vue.
178+
168179

169180

170181

vue-app/.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module.exports = {
1818
// add your custom rules here
1919
'rules': {
2020
'spaced-comment': 0,
21-
'quotes':0, 'space-before-function-paren':0, 'semi':0,
21+
'quotes':0, 'space-before-function-paren':0, 'semi':0, 'keyword-spacing':0,
2222
// allow paren-less arrow functions
2323
'arrow-parens': 0,
2424
// allow async-await

vue-app/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ <h1>todos</h1>
4747
<strong>{{ remaining }}</strong> {{ remaining | pluralize }} left
4848
</span>
4949
<ul class="filters">
50-
<li><a href="#/all" :class="{ selected: visibility == 'all' }">All</a></li>
51-
<li><a href="#/active" :class="{ selected: visibility == 'active' }">Active</a></li>
52-
<li><a href="#/completed" :class="{ selected: visibility == 'completed' }">Completed</a></li>
50+
<li><router-link to="/all" :class="{ selected: visibility == 'all' }">All</router-link></li>
51+
<li><router-link to="/active" :class="{ selected: visibility == 'active' }">Active</router-link></li>
52+
<li><router-link to="/completed" :class="{ selected: visibility == 'completed' }">Completed</router-link></li>
5353
</ul>
5454
<button class="clear-completed" @click="removeCompleted" v-show="todos.length > remaining">
5555
Clear completed

vue-app/src/main.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import Vue from 'vue'
2+
import VueRouter from 'vue-router';
23
import TodoModel from './todoModel'
34

45
import '../node_modules/todomvc-app-css/index.css'
56
// import './styles/styles.scss'
7+
Vue.use(VueRouter)
68

79
// Full spec-compliant TodoMVC with localStorage persistence
810
// and hash-based routing in ~120 effective lines of JavaScript.
911

1012
// visibility filters
11-
var filters = {
13+
const filters = {
1214
all: function (todos) {
1315
return todos
1416
},
@@ -24,8 +26,15 @@ var filters = {
2426
}
2527
}
2628

29+
const router = new VueRouter({
30+
routes: [
31+
{path: '/:filterBy'} //no component is needed, just want $route.params
32+
]
33+
})
34+
2735
// app Vue instance
2836
var app = new Vue({
37+
router,
2938
// app initial state
3039
data: {
3140
todos: [],
@@ -42,6 +51,10 @@ var app = new Vue({
4251
console.log("changed", todos)
4352
},
4453
deep: true
54+
},
55+
'$route': function () {
56+
console.log('filterBy', this.$route.params.filterBy)
57+
this.visibility = this.$route.params.filterBy
4558
}
4659
},
4760

@@ -142,22 +155,10 @@ var app = new Vue({
142155
created: function() {
143156
console.log("Ready")
144157
this.todos = TodoModel.query()
158+
console.log('filterBy', this.$route.params.filterBy)
159+
if(this.$route.params.filterBy) this.visibility = this.$route.params.filterBy
145160
}
146161
})
147162

148-
// handle routing
149-
function onHashChange () {
150-
var visibility = window.location.hash.replace(/#\/?/, '')
151-
if (filters[visibility]) {
152-
app.visibility = visibility
153-
} else {
154-
window.location.hash = ''
155-
app.visibility = 'all'
156-
}
157-
}
158-
159-
window.addEventListener('hashchange', onHashChange)
160-
onHashChange()
161-
162163
// mount
163164
app.$mount('.todoapp')

0 commit comments

Comments
 (0)