Skip to content

Commit 98f35aa

Browse files
committed
Major rewrite. Directly extends from v-component. Add options and navigate.
Add Vue.navigate(path) method to manually trigger a page change. The 'routes' hash now takes an 'options' object containing the debug and broadcast booleans, as well as 3 new props: click, base and hashbang. Extends the latest version of v-component.
1 parent 4848227 commit 98f35aa

File tree

12 files changed

+13790
-388
lines changed

12 files changed

+13790
-388
lines changed

README.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ vue-route
22
=======
33

44
Routing directive for Vue.js **(v0.11)**, inspired by ng-view.
5-
`v-transition` are supported, as well as keep-alive.
5+
Based on `v-component` thus benefits from `v-transition`, `keep-alive`, `wait-for`, `transition-mode`.
66

77
Allows you to declare your routes on the $root Vue object:
88

@@ -19,7 +19,9 @@ var root = new Vue({
1919
componentId: 'fg-work',
2020
afterUpdate: 'updateHeader'
2121
},
22-
broadcast: false
22+
options: {
23+
hashbang: true
24+
}
2325
},
2426
})
2527
@@ -33,7 +35,9 @@ with minimal markup:
3335
</body>
3436
3537
```
36-
It's heavily based on the `v-component` directive by @yyx990803 (on the [vuejs repo](https://github.com/yyx990803/vue/blob/0.11.0-rc3/src/directives/component.js)) so big up to him!
38+
39+
`vue-route` extends the `v-component` directive by @yyx990803 (on the [vuejs repo](https://github.com/yyx990803/vue/tree/master/src/directives/component.js)). Buy him a coffee if you can.
40+
3741
## Get started
3842

3943
**1.** Install with npm/component(1): `npm i vue-route --save` or `component install ayamflow/vue-route`.
@@ -61,17 +65,19 @@ Vue.use(route); // BOOM
6165
* *afterUpdate*: a callback (mehod or name of method on the vm) to call after effectively having changed to this route
6266
* *isDefault*: boolean indicating wether this page should be the default, in case of non-existing URL. Think of it as the `otherwise` from Angular, so basically a 404 or the home page.
6367

68+
Vue is augmented with an additional method, `Vue.navigate(path, [trigger])`. [trigger] is a boolean (defaults to true) that will `pushState` if true, `replaceState` otherwise.
69+
6470
* The router will emit events on your $root VM: `routing:started`, `routing:beforeUpdate`, `routing:afterUpdate`.
65-
* You can pass a `broadcast` boolean. If set to true, the events will be emitted using the $root `$broadcast` method, so all child VMs will receive the event until a handler `return false;`. If false, it uses `$emit`. Defaults to `false`.
66-
* You can pass a `debug` boolean to activate logging from the directive.
67-
* `keep-alive` works the same way than with `v-component`.
71+
* You can pass a `options` hash to pass configuration to the router:
72+
* `hashbang` boolean (defaults to false) to use '#!' urls
73+
* `click` boolean (defaults to true) to automatically bind all click to the router. Not that if `false`, you will need to explicitly call `Vue.navigate` method)
74+
* `base` string (defaults to '/') to specify the base path
75+
* `broadcast` boolean (defaults to false) if true the events will be emitted using the $root `$broadcast` method, so all child VMs will receive the event until a handler `return false;`. If false, it uses `$emit`.
76+
* `debug` boolean (defaults to false) to activate logging from the directive.
6877

6978
## Location context
7079
When the router emits an event, 2 parameters are passed: `location` and `oldLocation`. Like in Angular, it is an object containing some useful properties:
7180
* regexp: the route regexp, such as `/items/:itemId`
7281
* path: the current path, such as `/items/razor/`
7382
* params: a hash of the params from the route, here `{item: 'razor'}`
74-
* componentId: the componentId associated to the current route
75-
76-
## Todo
77-
* transition timing (out then in, in then out, ...)
83+
* componentId: the componentId associated to the current route

component.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-route",
3-
"version": "1.1.5",
3+
"version": "1.2.0",
44
"repository": "ayamflow/vue-route",
55
"description": "Routing directive for Vue.js, inspired by ng-view.",
66
"main": "src/index.js",
@@ -21,10 +21,10 @@
2121
"url": "https://github.com/ayamflow/vue-route/issues"
2222
},
2323
"dependencies": {
24-
"visionmedia/page.js": "~1.3.7"
24+
"visionmedia/page.js": "^1.5.0"
2525
},
2626
"development": {
27-
"yyx990803/vue": "~0.11.0",
28-
"substack/tape": "~3.0.2"
27+
"yyx990803/vue": "^0.11.3",
28+
"substack/tape": "^3.0.2"
2929
}
3030
}

package.json

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
{
22
"name": "vue-route",
3-
"version": "1.1.5",
3+
"version": "1.2.0",
44
"description": "Routing directive for Vue.js, inspired by ng-view.",
55
"main": "src/index.js",
66
"scripts": {
7-
"test": "browserify test/index.js | testling"
7+
"test": "browserify test/index.js | testling",
8+
"test-browser": "beefy test/index.js:test/build.js --debug=false"
89
},
910
"repository": {
1011
"type": "git",
@@ -23,24 +24,24 @@
2324
"url": "https://github.com/ayamflow/vue-route/issues"
2425
},
2526
"devDependencies": {
27+
"function-bind": "^1.0.2",
2628
"tape": "~3.0.2",
27-
"vue": "~0.11.0",
28-
"function-bind": "~1.0.2"
29+
"vue": "~0.11.3"
2930
},
3031
"testling": {
31-
"files": "test/index.js",
32-
"browsers": [
33-
"ie/9..latest",
34-
"chrome/22..latest",
35-
"firefox/16..latest",
36-
"safari/latest",
37-
"opera/11.0..latest",
38-
"iphone/6",
39-
"ipad/6",
40-
"android-browser/latest"
41-
]
42-
},
32+
"files": "test/index.js",
33+
"browsers": [
34+
"ie/9..latest",
35+
"chrome/22..latest",
36+
"firefox/16..latest",
37+
"safari/latest",
38+
"opera/11.0..latest",
39+
"iphone/6",
40+
"ipad/6",
41+
"android-browser/latest"
42+
]
43+
},
4344
"dependencies": {
44-
"page": "~1.3.7"
45+
"page": "^1.5.0"
4546
}
4647
}

src/component-api.js

Lines changed: 0 additions & 123 deletions
This file was deleted.

src/directive-api.js

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/index.js

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ var utils = require('./utils'),
44
page = require('page');
55

66
module.exports = function(Vue) {
7-
var component = require('./component-api')(Vue, page, utils),
8-
directive = require('./directive-api')(Vue, page, utils),
9-
route = require('./route-api')(Vue, page, utils),
10-
_ = Vue.util;
11-
12-
var routeDefinition = {
7+
var component = Vue.directive('component'),
8+
overrides = require('./overrides')(Vue),
9+
routing = require('./routing')(Vue, page, utils),
10+
_ = Vue.util;
1311

12+
var routeDefinition = _.extend({
1413
isLiteral: true,
1514

1615
// Vue event method, $emit or $broadcast
@@ -21,18 +20,27 @@ module.exports = function(Vue) {
2120
// Reference to $root.$options.routes
2221
routes: {},
2322

23+
// Options to be passed to the routing library
24+
options: {
25+
base: '/',
26+
hashbang: false,
27+
click: true
28+
},
29+
2430
// Location context, init (event with null) to avoid mutating it later (fast object)
2531
location: {
2632
regexp: null,
2733
path: null,
2834
componentId: null,
2935
params: null
3036
}
31-
};
37+
}, component);
38+
39+
// Extend the routing-related methods
40+
_.extend(routeDefinition, routing);
3241

33-
_.extend(routeDefinition, component); // Add methods from custom component directive
34-
_.extend(routeDefinition, directive); // Add directive lifecycle methods
35-
_.extend(routeDefinition, route); // Add routing methods
42+
// override some of components methods
43+
_.extend(routeDefinition, overrides);
3644

3745
Vue.directive('route', routeDefinition);
3846
};

0 commit comments

Comments
 (0)