Skip to content
Browse files

implement 'enter' and 'exit' methods to be used in browser

  • Loading branch information...
1 parent 3649ba6 commit 7e53ffd6a418c80b9b04c503ec5fede096964151 @nateps nateps committed
Showing with 21 additions and 19 deletions.
  1. +1 −1 lib/History.js
  2. +2 −4 lib/browser.js
  3. +4 −1 lib/index.js
  4. +14 −13 lib/router.js
View
2 lib/History.js
@@ -123,7 +123,7 @@ function renderOptions(e, path) {
return {
method: method
, url: path
- , previous: winLocation.pathname
+ , previous: winLocation.pathname + winLocation.search
, body: body
, form: form
}
View
6 lib/browser.js
@@ -1,10 +1,8 @@
// This is a dirty hack to ignore the require of connect.mime,
-// which is included by Express as of Express 3.0.0 Beta 3
+// which is included by Express as of Express 3.0.0
require.modules.connect = function() {
return {mime: null}
}
-// Express 3.0.0 Beta 1 & 2 used the mime npm module
-require.modules.mime = function() {}
var Route = require('express/lib/router/route')
, History = require('./History')
@@ -23,7 +21,7 @@ function setup(app, createPage, onRoute) {
page.redirect = redirect
page._routes = routes
- ;['get', 'post', 'put', 'del'].forEach(function(method) {
+ ;['get', 'post', 'put', 'del', 'enter', 'exit'].forEach(function(method) {
var queue = routes.queue[method] = []
, transitional = routes.transitional[method] = []
View
5 lib/index.js
@@ -8,11 +8,14 @@ router.setup = setup
function setup(app, createPage, onRoute) {
var routes = []
;['get', 'post', 'put', 'del'].forEach(function(method) {
- return app[method] = function(pattern, callback) {
+ app[method] = function(pattern, callback) {
routes.push([method, pattern, callback])
return app
}
})
+ ;['enter', 'exit'].forEach(function(method) {
+ app[method] = function() {}
+ })
function pageParams(req) {
var reqParams = req.params
View
27 lib/router.js
@@ -27,12 +27,12 @@ function mapRoute(from, params) {
return path + queryString
}
-function cancelRender(url, form, e) {
+function cancelRender(url, options, e) {
// Don't do anything if this is the result of an event, since the
// appropriate action will happen by default
- if (e) return
+ if (e || options.noNavigate) return
// Otherwise, manually perform appropriate action
- if (form) {
+ if (options.form) {
form._forceSubmit = true
return form.submit()
} else {
@@ -50,13 +50,12 @@ function render(page, options, e) {
, method = options.method
, body = options.body || {}
, previous = options.previous
- , form = options.form
, transitional = routes.transitional[method]
, queue = routes.queue[method]
function reroute(url) {
var path = url.replace(/\?.*/, '')
- renderQueued(previous, path, url, form, null, onMatch, transitional, queue, 0)
+ renderQueued(previous, path, url, options, null, onMatch, transitional, queue, 0)
}
function onMatch(path, url, i, route, renderNext, isTransitional) {
@@ -69,14 +68,16 @@ function render(page, options, e) {
for (key in routeParams) {
params[key] = routeParams[key]
}
+ params.previous = previous
params.url = url
params.body = body
params.query = query
+ params.method = method
page.params = params
function next(err) {
- if (err != null) return cancelRender(url, form)
- renderNext(previous, path, url, form, null, onMatch, transitional, queue, i)
+ if (err != null) return cancelRender(url, options)
+ renderNext(previous, path, url, options, null, onMatch, transitional, queue, i)
}
if (settings.debug) {
@@ -85,10 +86,10 @@ function render(page, options, e) {
try {
run(route, page, params, next, reroute, isTransitional)
} catch (err) {
- cancelRender(url, form)
+ cancelRender(url, options)
}
}
- return renderTransitional(previous, path, url, form, e, onMatch, transitional, queue, 0)
+ return renderTransitional(previous, path, url, options, e, onMatch, transitional, queue, 0)
}
function run(route, page, params, next, reroute, isTransitional) {
@@ -107,22 +108,22 @@ function run(route, page, params, next, reroute, isTransitional) {
onRoute(callbacks.callback, page, params, next, isTransitional)
}
-function renderTransitional(previous, path, url, form, e, onMatch, transitional, queue, i) {
+function renderTransitional(previous, path, url, options, e, onMatch, transitional, queue, i) {
var item
while (item = transitional[i++]) {
if (!item.to.match(path)) continue
if (!item.from.match(previous)) continue
return onMatch(path, url, i, item.to, renderTransitional, true)
}
- return renderQueued(previous, path, url, form, e, onMatch, transitional, queue, 0)
+ return renderQueued(previous, path, url, options, e, onMatch, transitional, queue, 0)
}
-function renderQueued(previous, path, url, form, e, onMatch, transitional, queue, i) {
+function renderQueued(previous, path, url, options, e, onMatch, transitional, queue, i) {
var route
while (route = queue[i++]) {
if (!route.match(path)) continue
return onMatch(path, url, i, route, renderQueued)
}
// Cancel rendering by this app if no routes match
- return cancelRender(url, form, e)
+ return cancelRender(url, options, e)
}

0 comments on commit 7e53ffd

Please sign in to comment.
Something went wrong with that request. Please try again.