Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

router: fix for popstate event on page load in Safari #154

Merged
merged 5 commits into from
Mar 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions lib/core/router.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,27 @@ module.exports = class KDRouter extends KDObject

back:-> if @visitedRoutes.length <= 1 then @clear() else history.back()

startListening:->
return no if @isListening # make this action idempotent
@isListening = yes
# we need to add a listener to the window's popstate event:
window.addEventListener 'popstate', @bound "popState"
return yes
startListening: do ->

readyStateBound = no

->
return no if @isListening # make this action idempotent

# Safari fires extra popstate event right after window is loaded
# this is to avoid this inconsistent initial firing
unless document.readyState is 'complete'
return if readyStateBound

readyStateBound = yes
return document.addEventListener 'readystatechange', =>
KD.utils.defer => @startListening() if document.readyState is 'complete'

@isListening = yes
window.addEventListener 'popstate', @bound "popState"

return yes


stopListening:->
return no unless @isListening # make this action idempotent
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kd.js",
"version": "1.1.19",
"version": "1.1.20",
"description": "a collection of ui widgets and other nice things",
"main": "build/lib/index.js",
"scripts": {
Expand Down