From 97d7122ff2a574ab53084b0a033c82b31baaf88a Mon Sep 17 00:00:00 2001 From: timwis Date: Sat, 4 Jun 2016 15:03:56 -0400 Subject: [PATCH 1/4] listen to hash changes --- index.js | 9 +++++++++ package.json | 3 ++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index ff6b089..b3e2e4d 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,8 @@ const history = require('sheet-router/history') const sheetRouter = require('sheet-router') const document = require('global/document') const href = require('sheet-router/href') +const hash = require('sheet-router/hash') +const hashMatch = require('hash-match') const sendAction = require('send-action') const mutate = require('xtend/mutable') const assert = require('assert') @@ -205,6 +207,13 @@ function appInit (opts) { // enable HTML5 history API if (opts.href !== false) pushLocationSub(href) if (opts.history !== false) pushLocationSub(history) + if (opts.hash !== false) { + pushLocationSub(function (navigate) { + hash(function (fragment) { + navigate(hashMatch(fragment)) + }) + }) + } return model diff --git a/package.json b/package.json index d9df638..551567e 100644 --- a/package.json +++ b/package.json @@ -20,8 +20,9 @@ "license": "MIT", "dependencies": { "global": "^4.3.0", + "hash-match": "^1.0.2", "send-action": "^1.1.0", - "sheet-router": "^3.0.0", + "sheet-router": "^3.1.0", "xhr": "^2.2.0", "xtend": "^4.0.1", "yo-yo": "^1.2.0" From 7e837a47eb4e90cefb8fa8c82472660a9c5c1a7a Mon Sep 17 00:00:00 2001 From: timwis Date: Sat, 4 Jun 2016 18:43:38 -0400 Subject: [PATCH 2/4] use hash history for initial location --- index.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index b3e2e4d..1a5274e 100644 --- a/index.js +++ b/index.js @@ -189,9 +189,16 @@ function choo () { // initial application state model // obj -> obj function appInit (opts) { + var initialLocation + if (opts.history !== false) { + initialLocation = document.location.href + } else { + initialLocation = hashMatch(document.location.hash) + } + const model = { namespace: 'app', - state: { location: document.location.href }, + state: { location: initialLocation }, subscriptions: [], reducers: { // handle href links @@ -206,8 +213,10 @@ function appInit (opts) { // enable catching links // enable HTML5 history API if (opts.href !== false) pushLocationSub(href) - if (opts.history !== false) pushLocationSub(history) - if (opts.hash !== false) { + if (opts.history !== false) { + pushLocationSub(history) + // otherwise enable hash history + } else { pushLocationSub(function (navigate) { hash(function (fragment) { navigate(hashMatch(fragment)) From 957f41bea96205f50aab9e993cb658231bffb341 Mon Sep 17 00:00:00 2001 From: timwis Date: Sat, 4 Jun 2016 19:53:05 -0400 Subject: [PATCH 3/4] use ternary, hash is opt-in --- index.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/index.js b/index.js index b0fa2b1..473f81e 100644 --- a/index.js +++ b/index.js @@ -192,12 +192,9 @@ function choo () { // initial application state model // obj -> obj function appInit (opts) { - var initialLocation - if (opts.history !== false) { - initialLocation = document.location.href - } else { - initialLocation = hashMatch(document.location.hash) - } + const initialLocation = (opts.hash === true) + ? hashMatch(document.location.hash) + : document.location.href const model = { namespace: 'app', @@ -213,18 +210,18 @@ function appInit (opts) { } } - // enable catching links - // enable HTML5 history API - if (opts.href !== false) pushLocationSub(href) - if (opts.history !== false) { - pushLocationSub(history) - // otherwise enable hash history - } else { + // if hash routing explicitly enabled, subscribe to it + if (opts.hash === true) { pushLocationSub(function (navigate) { hash(function (fragment) { navigate(hashMatch(fragment)) }) }) + // otherwise, subscribe to HTML5 history API + } else { + if (opts.history !== false) pushLocationSub(history) + // enable catching links + if (opts.href !== false) pushLocationSub(href) } return model From fed766843ceb8bfa588fc133890885ed54d95c89 Mon Sep 17 00:00:00 2001 From: timwis Date: Wed, 8 Jun 2016 05:59:24 -0400 Subject: [PATCH 4/4] add opts.hash to docs --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index f5df7eb..829d455 100644 --- a/README.md +++ b/README.md @@ -653,6 +653,10 @@ following values: - __opts.href:__ default: `true`. Handle all relative `` clicks and update internal `state.location` accordingly. +- __opts.hash:__ default: `false`. Enable a `subscription` to the hash change + event, updating the internal `state.location` state whenever the URL hash + changes (eg `localhost/#posts/123`). Enabling this option automatically + disables `opts.history` and `opts.href`. ## Errors ### Could not find DOM node (#id) to update