Skip to content

Commit 73b9913

Browse files
committed
more es6
1 parent ebb92b0 commit 73b9913

File tree

12 files changed

+48
-38
lines changed

12 files changed

+48
-38
lines changed

build/karma.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function (config) {
1313
{
1414
test: /\.js$/,
1515
exclude: /test|node_modules|vue\/src/,
16-
loader: 'babel'
16+
loader: 'babel?optional[]=runtime'
1717
}
1818
],
1919
postLoaders: [

example/advanced/app.vue

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
</template>
2828

2929
<script>
30-
module.exports = {
31-
data: function () {
30+
export default {
31+
data () {
3232
return {
3333
authenticating: false
3434
}

example/advanced/components/inbox/index.vue

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313
// 1. return a boolean
1414
// 2. return a promise that resolves to a boolean
1515
// 3. explicitly call transition.next() or abort()
16-
canActivate: function (transition) {
16+
canActivate (transition) {
1717
console.log('inbox canActivate?')
1818
if (transition.from.path === '/about') {
1919
alert('cannot navigate from /about to /inbox')
@@ -25,23 +25,32 @@ module.exports = {
2525
},
2626
2727
// same deal with beforeActicate
28-
canDeactivate: function (transition) {
28+
canDeactivate (transition) {
2929
return confirm('Are you sure you want to leave inbox?')
3030
},
3131
32-
activate: function (transition) {
32+
// activate hook is called when the route is matched
33+
// and the component has been created.
34+
// this hook controls the timing of the component
35+
// switching - the switching won't start until this
36+
// hook is resolved.
37+
activate () {
3338
console.log('activating inbox...')
34-
transition.next()
39+
return new Promise((resolve) => {
40+
console.log('inbox activated.')
41+
resolve()
42+
})
3543
},
3644
3745
// for doing cleanups
38-
deactivate: function (transition) {
39-
console.log('inbox deactivate')
40-
transition.next()
46+
// destructuring can make hooks cleaner
47+
deactivate ({ next }) {
48+
console.log('inbox deactivated.')
49+
next()
4150
}
4251
},
4352
44-
data: function () {
53+
data () {
4554
return {
4655
message: {}
4756
}

example/advanced/components/inbox/message.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@ module.exports = {
1818
// 1. return a promise
1919
// 2. explicitly call transition.next() or
2020
// transition.abort(reason)
21-
data: function (transition) {
21+
data ({ to, next }) {
2222
// "this" is available
2323
var params = {
24-
id: transition.to.params.messageId
24+
id: to.params.messageId
2525
}
2626
2727
// callback based
28-
messagesSerivce.get(params, function (err, message) {
28+
messagesSerivce.get(params, (err, message) => {
2929
if (err) {
3030
// handle error, e.g. display a warning
3131
} else {
32-
transition.next({
32+
next({
3333
message: message
3434
})
3535
}
@@ -43,7 +43,7 @@ module.exports = {
4343
},
4444
4545
// default state
46-
data: function () {
46+
data () {
4747
return {
4848
message: {}
4949
}

example/advanced/index.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
// warning: vue-router requires Vue 0.12.10+
2-
var Vue = require('vue')
3-
var VueRouter = require('../../src')
2+
import Vue from 'vue'
3+
import VueRouter from '../../src'
4+
import { configRouter } from './route-config'
5+
require('es6-promise').polyfill()
46

57
// install router
68
Vue.use(VueRouter)
79

810
// create router
9-
var router = new VueRouter({
11+
const router = new VueRouter({
1012
history: true,
1113
saveScrollPosition: true
1214
})
1315

1416
// configure router
15-
require('./route-config')(router)
17+
configRouter(router)
1618

1719
// boostrap the app
18-
var App = Vue.extend(require('./app.vue'))
20+
const App = Vue.extend(require('./app.vue'))
1921
router.start(App, '#app')
2022

2123
// just for debugging

example/advanced/route-config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
module.exports = function (router) {
1+
export function configRouter (router) {
22

33
// normal routes
44
router.map({
@@ -68,10 +68,10 @@ module.exports = function (router) {
6868
// 1. return a boolean
6969
// 2. return a Promise that resolves to a boolean
7070
// 3. call transition.next() or transition.abort()
71-
router.beforeEach(function (transition) {
71+
router.beforeEach((transition) => {
7272
if (transition.to.path === '/forbidden') {
7373
router.app.authenticating = true
74-
setTimeout(function () {
74+
setTimeout(() => {
7575
router.app.authenticating = false
7676
alert('this route is forbidden by a global before hook')
7777
transition.abort()

example/advanced/services/messages.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// just a mock
2-
module.exports = {
3-
get: function (params, cb) {
4-
setTimeout(function () {
2+
export default {
3+
get (params, cb) {
4+
setTimeout(() => {
55
cb(null, {
66
id: params.id,
77
text: 'Hello this is a message'

example/advanced/webpack.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ module.exports = {
1313
{
1414
test: /\.vue$/,
1515
loader: vue.withLoaders({
16-
script: 'babel'
16+
js: 'babel?optional[]=runtime'
1717
})
1818
},
1919
{
2020
test: /\.js$/,
2121
exclude: /node_modules|vue\/src/,
22-
loader: 'babel'
22+
loader: 'babel?optional[]=runtime'
2323
}
2424
]
2525
},

src/history/abstract.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { resolvePath } from '../util'
22

33
export default class AbstractHistory {
44

5-
constructor (options) {
6-
this.onChange = options.onChange
5+
constructor ({ onChange }) {
6+
this.onChange = onChange
77
this.currentPath = '/'
88
}
99

src/history/hash.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { resolvePath } from '../util'
22

33
export default class HashHistory {
44

5-
constructor (options) {
6-
this.hashbang = options.hashbang
7-
this.onChange = options.onChange
5+
constructor ({ hashbang, onChange }) {
6+
this.hashbang = hashbang
7+
this.onChange = onChange
88
}
99

1010
start () {

src/history/html5.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ const hashRE = /#.*$/
33

44
export default class HTML5History {
55

6-
constructor (options) {
7-
let root = options.root
6+
constructor ({ root, onChange }) {
87
if (root) {
98
// make sure there's the starting slash
109
if (root.charAt(0) !== '/') {
@@ -16,7 +15,7 @@ export default class HTML5History {
1615
} else {
1716
this.root = null
1817
}
19-
this.onChange = options.onChange
18+
this.onChange = onChange
2019
// check base tag
2120
let baseEl = document.querySelector('base')
2221
this.base = baseEl && baseEl.getAttribute('href')

test/unit/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
{
1010
test: /\.js$/,
1111
exclude: /test|node_modules|vue\/src/,
12-
loader: 'babel'
12+
loader: 'babel?optional[]=runtime'
1313
}
1414
]
1515
}

0 commit comments

Comments
 (0)