1
1
var Recognizer = require ( 'route-recognizer' )
2
2
var hasPushState = typeof history !== 'undefined' && history . pushState
3
+ var installed = false
4
+ var Vue
3
5
4
6
/**
5
7
* Router constructor
@@ -11,9 +13,9 @@ var hasPushState = typeof history !== 'undefined' && history.pushState
11
13
*/
12
14
13
15
function VueRouter ( options ) {
16
+ this . app = null
14
17
this . _recognizer = new Recognizer ( )
15
18
this . _started = false
16
- this . _vm = null
17
19
this . _currentPath = null
18
20
this . _notfoundHandler = null
19
21
this . _root = null
@@ -31,13 +33,29 @@ function VueRouter (options) {
31
33
this . _pushstate = ! ! ( hasPushState && options && options . pushstate )
32
34
}
33
35
34
- var p = VueRouter . prototype
36
+ /**
37
+ * Installation interface.
38
+ * Install the necessary directives.
39
+ */
40
+
41
+ VueRouter . install = function ( ExternalVue ) {
42
+ if ( installed ) {
43
+ warn ( 'vue-router has already been installed.' )
44
+ return
45
+ }
46
+ Vue = ExternalVue
47
+ installed = true
48
+ require ( './view' ) ( Vue )
49
+ require ( './link' ) ( Vue )
50
+ }
35
51
36
52
//
37
53
// Public API
38
54
//
39
55
//
40
56
57
+ var p = VueRouter . prototype
58
+
41
59
/**
42
60
* Register a map of top-level paths.
43
61
*/
@@ -123,34 +141,64 @@ p.go = function (path, options) {
123
141
/**
124
142
* Start the router.
125
143
*
126
- * @param {Vue } vm
144
+ * @param {VueConstructor } App
145
+ * @param {String|Element } container
127
146
*/
128
147
129
- p . start = function ( vm ) {
148
+ p . start = function ( App , container ) {
149
+ if ( ! installed ) {
150
+ throw new Error (
151
+ 'Please install vue-router with Vue.use() before ' +
152
+ 'starting the router.'
153
+ )
154
+ }
130
155
if ( this . _started ) {
156
+ warn ( 'vue-router has already been started.' )
131
157
return
132
158
}
133
159
this . _started = true
134
- this . _vm = this . _vm || vm
135
- if ( ! this . _vm ) {
136
- throw new Error (
137
- 'vue-router must be started with a root Vue instance.'
138
- )
160
+ if ( ! this . app ) {
161
+ if ( ! App || ! container ) {
162
+ throw new Error (
163
+ 'Must start vue-router with a component and a ' +
164
+ 'root container.'
165
+ )
166
+ }
167
+ this . _appContainer = container
168
+ this . _appConstructor = typeof App === 'function'
169
+ ? App
170
+ : Vue . extend ( App )
139
171
}
140
172
if ( this . _pushstate ) {
141
- this . initHistoryMode ( )
173
+ this . _initHistoryMode ( )
142
174
} else {
143
- this . initHashMode ( )
175
+ this . _initHashMode ( )
144
176
}
145
177
}
146
178
179
+ /**
180
+ * Stop listening to route changes.
181
+ */
182
+
183
+ p . stop = function ( ) {
184
+ var event = this . _pushstate
185
+ ? 'popstate'
186
+ : 'hashchange'
187
+ window . removeEventListener ( event , this . _onRouteChange )
188
+ this . _started = false
189
+ }
190
+
191
+ //
192
+ // Private Methods
193
+ //
194
+
147
195
/**
148
196
* Initialize hash mode.
149
197
*/
150
198
151
- p . initHashMode = function ( ) {
199
+ p . _initHashMode = function ( ) {
152
200
var self = this
153
- this . onRouteChange = function ( ) {
201
+ this . _onRouteChange = function ( ) {
154
202
// format hashbang
155
203
var hash = location . hash
156
204
if ( self . _hashbang && hash && hash . charAt ( 1 ) !== '!' ) {
@@ -166,42 +214,25 @@ p.initHashMode = function () {
166
214
url = decodeURI ( url )
167
215
self . _match ( url )
168
216
}
169
- window . addEventListener ( 'hashchange' , this . onRouteChange )
170
- this . onRouteChange ( )
217
+ window . addEventListener ( 'hashchange' , this . _onRouteChange )
218
+ this . _onRouteChange ( )
171
219
}
172
220
173
221
/**
174
222
* Initialize HTML5 history mode.
175
223
*/
176
224
177
- p . initHistoryMode = function ( ) {
225
+ p . _initHistoryMode = function ( ) {
178
226
var self = this
179
- this . onRouteChange = function ( ) {
227
+ this . _onRouteChange = function ( ) {
180
228
var url = location . pathname + location . search
181
229
url = decodeURI ( url )
182
230
self . _match ( url )
183
231
}
184
- window . addEventListener ( 'popstate' , this . onRouteChange )
185
- this . onRouteChange ( )
186
- }
187
-
188
- /**
189
- * Stop listening to route changes.
190
- */
191
-
192
- p . stop = function ( ) {
193
- var event = this . _pushstate
194
- ? 'popstate'
195
- : 'hashchange'
196
- window . removeEventListener ( event , this . onRouteChange )
197
- this . _vm . route = null
198
- this . _started = false
232
+ window . addEventListener ( 'popstate' , this . _onRouteChange )
233
+ this . _onRouteChange ( )
199
234
}
200
235
201
- //
202
- // Private Methods
203
- //
204
-
205
236
/**
206
237
* Add a route containing a list of segments to the internal
207
238
* route recognizer. Will be called recursively to add all
@@ -211,6 +242,7 @@ p.stop = function () {
211
242
* @param {Object } config
212
243
* @param {Array } segments
213
244
*/
245
+
214
246
p . _addRoute = function ( path , config , segments ) {
215
247
segments . push ( {
216
248
path : path ,
@@ -237,6 +269,7 @@ p._addRoute = function (path, config, segments) {
237
269
*
238
270
* @param {String } path
239
271
*/
272
+
240
273
p . _match = function ( path ) {
241
274
if ( path === this . _currentPath ) {
242
275
return
@@ -272,7 +305,16 @@ p._match = function (path) {
272
305
_matchedCount : 0 ,
273
306
_router : this
274
307
}
275
- this . _vm . $set ( 'route' , context )
308
+ if ( ! this . app ) {
309
+ this . app = new this . _appConstructor ( {
310
+ el : this . _appContainer ,
311
+ data : {
312
+ route : context
313
+ }
314
+ } )
315
+ } else {
316
+ this . app . route = context
317
+ }
276
318
}
277
319
278
320
/**
@@ -281,6 +323,7 @@ p._match = function (path) {
281
323
* @param {String } hash
282
324
* @param {Boolean } replace
283
325
*/
326
+
284
327
function setHash ( hash , replace ) {
285
328
if ( replace ) {
286
329
var urlLength = location . href . length - location . hash . length
@@ -292,26 +335,15 @@ function setHash (hash, replace) {
292
335
}
293
336
294
337
/**
295
- * Installation interface.
296
- * Install the necessary directives.
338
+ * Warning (check console for IE9)
339
+ *
340
+ * @param {String } msg
297
341
*/
298
342
299
- var installed = false
300
- VueRouter . install = function ( Vue ) {
301
- if ( installed ) {
302
- Vue . util . warn && Vue . util . warn (
303
- 'vue-router has already been installed.'
304
- )
305
- return
343
+ function warn ( msg ) {
344
+ if ( typeof console !== 'undefined' ) {
345
+ console . warn ( msg )
306
346
}
307
- installed = true
308
- require ( './view' ) ( Vue )
309
- require ( './link' ) ( Vue )
310
- }
311
-
312
- // Auto-install if loaded
313
- if ( typeof Vue !== 'undefined' ) {
314
- Vue . use ( VueRouter )
315
347
}
316
348
317
349
module . exports = VueRouter
0 commit comments