Skip to content

Commit

Permalink
feat: add base option for applications not using hash and having a no…
Browse files Browse the repository at this point in the history
…n-empty base path

Issue #9
  • Loading branch information
troch committed Jul 23, 2015
1 parent 7820a3b commit 42ea04e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 15 deletions.
19 changes: 13 additions & 6 deletions modules/Router5.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ export default class Router5 {
hashPrefix: ''
}
Object.keys(opts).forEach(opt => this.options[opt] = opts[opt])
this.base = window.location.pathname.replace(/\/$/, '')

this._setBase()
return this
}

/**
* @private
*/
_setBase() {
this.base = this.options.base || window.location.pathname.replace(/\/$/, '')
}

/**
* Set an option value
* @param {String} opt The option to set
Expand All @@ -38,6 +44,7 @@ export default class Router5 {
*/
setOption(opt, val) {
this.options[opt] = val
if (opt === 'base') this._setBase()
return this
}

Expand Down Expand Up @@ -117,7 +124,7 @@ export default class Router5 {
} else if (opts.defaultRoute) {
navigateToDefault()
} else {
cb()
cb(null)
}
// Listen to popstate
return this
Expand Down Expand Up @@ -371,7 +378,7 @@ export default class Router5 {
this._invokeListeners('=' + toState.name, toState, fromState)
this._invokeListeners('*', toState, fromState)

if (done) done(null, true)
if (done) done(null)
})

this._tr = tr
Expand All @@ -383,7 +390,7 @@ export default class Router5 {
* @param {String} name The route name
* @param {Object} [params={}] The route params
* @param {Object} [opts={}] The route options (replace, reload)
* @param {Function} done A optional callback (err, res) to call when transition has been performed
* @param {Function} done A optional callback(err) to call when transition has been performed
* either successfully or unsuccessfully.
* @return {Function} A cancellation function
*/
Expand Down Expand Up @@ -416,7 +423,7 @@ export default class Router5 {
}

window.history[opts.replace ? 'replaceState' : 'pushState'](this.lastStateAttempt, '', url)
if (done) done(null, true)
if (done) done(null)
})
}
}
10 changes: 5 additions & 5 deletions modules/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ export default function asyncProcess(functions, toState, fromState, callback, al
let len = remainingSteps[0].length
let res = remainingSteps[0](toState, fromState, done)

if (typeof res === 'boolean') done(!res, res);
if (typeof res === 'boolean') done(res ? null : true);

else if (res && typeof res.then === 'function') {
res.then(() => done(null, true), () => done(true, null))
res.then(() => done(null), () => done(true))
}

else if (len < 3 && allowNoResult) done(null, true)
else if (len < 3 && allowNoResult) done(null)

return false
}

let iterate = (err, res) => {
let iterate = (err) => {
if (err) callback(err)
else {
remainingSteps = remainingSteps.slice(1)
Expand All @@ -28,7 +28,7 @@ export default function asyncProcess(functions, toState, fromState, callback, al

let next = () => {
let finished = processFn(iterate)
if (finished) callback(null, true)
if (finished) callback(null)
}

next()
Expand Down
8 changes: 4 additions & 4 deletions modules/transition.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let nameToIDs = name => {
export default function transition(router, toState, fromState, callback) {
let cancelled = false
let cancel = () => cancelled = true
let done = (err, res) => callback(cancelled ? constants.TRANSITION_CANCELLED : err, res)
let done = (err) => callback(cancelled ? constants.TRANSITION_CANCELLED : err)

let i
let fromStateIds = fromState ? nameToIDs(fromState.name) : []
Expand All @@ -35,7 +35,7 @@ export default function transition(router, toState, fromState, callback) {

asyncProcess(
canDeactivateFunctions, toState, fromState,
(err, res) => cb(err ? constants.CANNOT_DEACTIVATE : null, res)
err => cb(err ? constants.CANNOT_DEACTIVATE : null)
)
}
}
Expand All @@ -49,7 +49,7 @@ export default function transition(router, toState, fromState, callback) {

asyncProcess(
canActivateFunctions, toState, fromState,
(err, res) => cb(err ? constants.CANNOT_ACTIVATE : null, res)
err => cb(err ? constants.CANNOT_ACTIVATE : null)
)
}
}
Expand All @@ -60,7 +60,7 @@ export default function transition(router, toState, fromState, callback) {
let listeners = router._cbs['^' + intersection] || []
asyncProcess(
listeners, toState, fromState,
(err, res) => cb(err ? constants.NODE_LISTENER_ERR : null, res),
err => cb(err ? constants.NODE_LISTENER_ERR : null),
true
)
}
Expand Down

0 comments on commit 42ea04e

Please sign in to comment.