page.js icon indicating copy to clipboard operation
page.js copied to clipboard

click and dom context

Open pgherveou opened this issue 12 years ago • 6 comments

TJ I am using page click option to handle click event

my links have data attributes

<a href="/some-path" data-transition="slide-left">my link</a>

So would it make sense to add a reference to the dom element in the state object so I can use data attributes? Or Should I write my own custom onClick method for that ?

pgherveou avatar Mar 11 '13 13:03 pgherveou

I'd find this very useful too.

We have the situation, where we allow links to open in a lightbox, but still change the URL. We do that with a special target tag. The easiest for our case would certainly be to have access to the dom element.

However that should probably not be the responsibility of page.js. Still I would like to be able to access the contents of that target attribute in my middlewares. One solution I can think of is to be able to manipulate the context when calling #page() inside my own click handler.

I was going to suggest to implement that. But it turns out, that something similar exists. You can prevent #start() from calling #replace() and then call #replace() yourself and pass a state object like so:

page.start( { dispatch: false } );
page.replace( location.pathname + location.search, { target: "#selector" }, true, true );

Thats for starting, after that you can just use #show().

// inside your click handler
page.show( $this.attr( "href" ),  { target: $this.attr( "data-target" ) } );

Just ignore that I assume jQuery ;) the idea stays the same.

jostsg avatar Apr 03 '13 14:04 jostsg

The reference wouldn't be bad, but I am of the opinion that page shouldn't do any delegation at all, and that should be a separate component.

rauchg avatar Apr 03 '13 16:04 rauchg

it seemed relatively non-leaky at the time but we're investigating better patterns for client-side routing, there's definitely a few gotchas/annoyances with page.js ATM

tj avatar Apr 03 '13 17:04 tj

Although having links that automatically trigger pushState and a page() handler IS indeed very useful and I'd say covers a large percentage of use cases, so the functionality somehow needs to stay, maybe slightly different impl

rauchg avatar Apr 03 '13 18:04 rauchg

it's stil pretty easy to do the inverse and .preventDefault() to disable page.js for links, the 80% use-case would be to dispatch so that still kinda works

tj avatar Apr 03 '13 18:04 tj

we could always have some data-page sort of thing to opt-in/out depending on what the default is. I'm using target="_self" as a hack right now to bypass page haha

tj avatar Apr 03 '13 19:04 tj