defunkt / jquery-pjax

pushState + ajax = pjax
https://pjax.herokuapp.com
MIT License
16.73k stars 1.97k forks source link

No event is fired before current contents are cached #451

Open tremby opened 9 years ago

tremby commented 9 years ago

It would be very useful to be able to run some destruction logic on the page before it is cached. But at present pjax:start fires after cachePush so as far as I can tell there's no way to do this for a back/forward event.

tremby commented 9 years ago

This commit adds an event. It's working well for me so far. https://github.com/tremby/jquery-pjax/commit/62fc5afea87cfcff62be70ade5546d3f1f31dc9d

mislav commented 9 years ago

How about pjax:start

tremby commented 9 years ago

At 2014-11-04 12:47:33 -0800, Mislav Marohnić wrote:

How about pjax:start

No, pjax:start fires after the current contents are cached.

mislav commented 9 years ago

pjax:start fires first before anything is done but I agree it's not a perfect phase since you would perform destruction before there's even a pjax response from the server.

What sort of destruction do you need to perform before the HTML is pushed to cache?

tremby commented 9 years ago

https://github.com/defunkt/jquery-pjax/blob/08a785dc367e1d28c54ac1fd3e77acf84f960f86/jquery.pjax.js#L365-373 It is not fired "before anything is done".

In this particular case I need to destruct a JS-controlled scrollbar. I can't skip destruction and then when coming back skip the initialization if it has already been done, because its various event listeners are now missing. And I can't just initialize again, because the first initialization has added elements and classes, and running again makes a mess (scrollpane within scrollpane, etc).

(edit: changed link to point at the codebase as it was at the time when I posted this comment, so the right lines are highlighted)

mislav commented 9 years ago

It is not fired "before anything is done".

Huh, interesting. I didn't know we cache before the xhr is started

mislav commented 9 years ago

You could react to a "pjax:click" but that won't handle manual pjax() calls or the back button.

@josh what do you think about this event?

tremby commented 9 years ago

Correct, that's the problem.

tremby commented 9 years ago

Perhaps the start event could just be moved to earlier? I'm on another project right now and don't know off hand if the start event is fired when going back/forward, but I'd need to hook in before the cache is touched at that time too, not just when navigating to a new page.

olemoign commented 9 years ago

@mislav Actually, using pjax:beforeSend will handle pjax() calls but not handling popstate events means it can't really be used.

In my project (where I use dataTables, with the same needs that @tremby), I've added a pjax:beforeCaching event in the onPjaxPopstate function which works well.

korotovsky commented 8 years ago

Any progress is here? I use https://en.bem.info/libs/bem-core/v2.8.0/ (i-bem) library and it adds *_js_inited to each BEM-block when it initializes. I have a listener which listen for pjax:end and initializes the current container with BEM-blocks. BUT I also have to do BEMDOM.detach() to destruct all event listeners in old container before it will be cached and after XHR is completed. Because UI should respond while request is in progress.

Currently: I would like to get any point of extension for modifying old (current) container before caching. Ideally: It would be nice to get any point of extension and cache old container after XHR has been succeed.

// cc @mislav

korotovsky commented 8 years ago

@mislav BTW, there is no way to use pjax:popstate on "forward" to destruct the old container because cachePop is also stores the current content to cache stack. And now I can't imagine how to solve it even with workaround

tremby commented 8 years ago

@korotovsky, you could try using my fork, mentioned above. I'm using it in several production websites, some of which are fairly high-traffic.

I just noticed, incidentally, that the relevant commit has been merged in to the heroku branch of this official repository, so it would appear it has support from the pjax team.

tremby commented 8 years ago

It seems I was mistaken. Not sure why it shows that way on the graph when I look at my repository locally.

screen shot 2015-11-16 at 10 03 11

(Near the bottom. Weird.)