igor-alexandrov / wiselinks

If Turbolinks are not enough for you. Wiselinks makes your application work faster.
MIT License
724 stars 89 forks source link

Change History state without reloading content? #87

Open wideopenspaces opened 10 years ago

wideopenspaces commented 10 years ago

Hello!

Wiselinks has been pretty much perfect for my application, but I have a situation where I'd like to push information into the History stack without causing a reload (namely, 'pagination' via infinite scroll)

What I want to do is:

User goes to

http://example.org/home

and then scrolls down a ways, and a second page of content is loaded. I want the url in the title bar and the History stack to reflect this:

http://example.org/home?page=2

What currently happens, though, is that when I attempt to change history:

History.pushState( { page: 2}, 'page the 2nd!', 'http://example.org/home?page=2' );

wiselinks kicks in and replaces my existing content with the content of page 2.

I'm thinking that I just need to provide a way to disable wiselinks' event listener for the pushState(?) event while I'm making the change, and then re-enable it afterward. Or would it pick up the new URL in the history the moment I re-enable?

kshmir commented 9 years ago

In this branch I am attempting to fix what you are reporting.

https://github.com/kshmir/wiselinks/tree/no_wiselinks_flag

mjrk commented 9 years ago

@kshmir thanks for sharing your approach!

However, there is a small pitfall in storing no_wiselinks (or skipLoadingPage in my case) in the state data object. If you continue and use the back button afterwards, your page won't be loaded because the data.no_wiselinks is still set. Therefore I implemented the feature like this

originalCall = window._Wiselinks.Page.prototype._call
window._Wiselinks.Page.prototype._call = (state) ->
  return if Wiselinks.skipLoadingPage
  originalCall.apply(this, arguments)

Wiselinks.skipLoadingPage = true
  # do your state changes
Wiselinks.skipLoadingPage = false
kshmir commented 9 years ago

Amazing! I didnt stop to bother around that issue sorry for not mentioning it

On Wed, Nov 26, 2014, 12:33 mjrk notifications@github.com wrote:

@kshmir https://github.com/kshmir thanks for sharing your approach!

However, there is a small pitfall in storing no_wiselinks (or skipLoadingPage in my case) in the state data object. If you continue and use the back button afterwards, your page won't be loaded because the data.no_wiselinks is still set. Therefore I implemented the feature like this

originalCall = window._Wiselinks.Page.prototype._call window._Wiselinks.Page.prototype._call = (state) -> return if Wiselinks.skipLoadingPage originalCall.apply(this, arguments)

Wiselinks.skipLoadingPage = true

do your state changes

Wiselinks.skipLoadingPage = false

— Reply to this email directly or view it on GitHub https://github.com/igor-alexandrov/wiselinks/issues/87#issuecomment-64662678 .