Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fix so that createPage is called lazily on browser

  • Loading branch information...
commit ab9fe45d56a7d3fe7cf95e2aca995ef23317c97b 1 parent ba44b00
@nateps nateps authored
Showing with 39 additions and 22 deletions.
  1. +28 −8 lib/History.js
  2. +11 −14 lib/browser.js
View
36 lib/History.js
@@ -20,10 +20,12 @@ if (winHistory.replaceState) {
module.exports = History
-function History(page) {
- this.page = page
+function History(createPage, routes) {
+ this._createPage = createPage
+ this._routes = routes
+
if (winHistory.pushState) {
- addListeners(this, page)
+ addListeners(this)
} else {
// TODO: Calling push, replace, and refresh should update
// window.location when pushState is not supported
@@ -43,7 +45,7 @@ History.prototype = {
// Rerender the current url locally
, refresh: function() {
var path = routePath(winLocation.href)
- renderRoute(this.page, {url: path, method: 'get'})
+ renderRoute(this.page(), {url: path, method: 'get'})
}
, back: function() {
@@ -74,7 +76,25 @@ History.prototype = {
state.$method = options.method
winHistory[historyMethod](state, null, url)
currentPath = winLocation.pathname + winLocation.search
- if (render) renderRoute(this.page, options, e)
+ if (render) renderRoute(this.page(), options, e)
+ }
+
+, page: function() {
+ if (this._page) return this._page
+
+ var page = this._page = this._createPage()
+ , history = this
+
+ function redirect(url) {
+ if (url === 'back') return history.back()
+ // TODO: Add support for `basepath` option like Express
+ if (url === 'home') url = '\\'
+ history.replace(url, true)
+ }
+
+ page.redirect = redirect
+ page._routes = this._routes
+ return page
}
}
@@ -129,7 +149,7 @@ function renderOptions(e, path) {
}
}
-function addListeners(history, page) {
+function addListeners(history) {
function onClick(e) {
// Detect clicks on links
@@ -184,7 +204,7 @@ function addListeners(history, page) {
options.method = state.$method
// Note that the post body is only sent on the initial reqest
// and it is empty if the state is later popped
- return renderRoute(page, options)
+ return renderRoute(history.page(), options)
}
// The state object will be null for states created by jump links.
@@ -195,7 +215,7 @@ function addListeners(history, page) {
, el, id
if (~hashIndex && currentPath !== previous) {
options.method = 'get'
- renderRoute(page, options)
+ renderRoute(history.page(), options)
id = url.slice(hashIndex + 1)
if (el = doc.getElementById(id) || doc.getElementsByName(id)[0]) {
el.scrollIntoView()
View
25 lib/browser.js
@@ -13,19 +13,23 @@ router.setup = setup
function setup(app, createPage, onRoute) {
var routes = {
- queue: {}
- , transitional: {}
- }
- , page = createPage()
- , history = page._history = app.history = new History(page)
- page.redirect = redirect
- page._routes = routes
+ queue: {}
+ , transitional: {}
+ }
+ app.history = new History(createPage, routes)
;['get', 'post', 'put', 'del', 'enter', 'exit'].forEach(function(method) {
var queue = routes.queue[method] = []
, transitional = routes.transitional[method] = []
app[method] = function(pattern, callback, callback2) {
+ if (Array.isArray(pattern)) {
+ pattern.forEach(function(item) {
+ app[method](item, callback, callback2)
+ })
+ return app
+ }
+
var callbacks = {onRoute: onRoute}
if (isTransitional(pattern)) {
@@ -56,10 +60,3 @@ function setup(app, createPage, onRoute) {
}
})
}
-
-function redirect(url) {
- if (url === 'back') return this._history.back()
- // TODO: Add support for `basepath` option like Express
- if (url === 'home') url = '\\'
- this._history.replace(url, true)
-}
Please sign in to comment.
Something went wrong with that request. Please try again.