Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

access what is set by the selectin.on() #216

Closed
lanyjie opened this issue Jul 8, 2011 · 4 comments
Closed

access what is set by the selectin.on() #216

lanyjie opened this issue Jul 8, 2011 · 4 comments
Milestone

Comments

@lanyjie
Copy link

lanyjie commented Jul 8, 2011

I wonder if it is possible to access the event handler set by the selection.on() method. I tried quite a few but all failed. for example, I would like to blink a path by repeatedly call the same transition function:

d3.select('#sensor')
.on('mouseover', function(){
d3.select('#actor')
.transition()
.each('start', function(){d3.select(this).attr('stroke-width',1); })
.attr('stroke-width', 4).delay(1000)
.each('end', function(){ this.onmoueover(); })

The intention of the above code is to recursively call the function set by the on() method. Is there anyway to get it done? I also tried to use selection.on('mouseover') to access the set function, without luck.

Regards,

Lane

@mbostock
Copy link
Member

mbostock commented Jul 8, 2011

There's on API for doing this currently, but you can access where we store the handler: this.__onmouseover. If you use a namespaced handler, then it's this["__onmouseover.name"].

Another thing you can do that's easy is to not use an anonymous function, and just call your function. For example:

d3.select("#sensor").on("mouseover", mouseover);
d3.select("#actor").transition().each("end", mouseover);

function mouseover() {
  // do some stuff here
}

Lastly, issue #100 covers a request to trigger events on other objects, which would be another way of calling their event handlers. In this case, you could trigger a "mouseover" event on the desired elements, and all the associated event handlers would be run.

@lanyjie
Copy link
Author

lanyjie commented Jul 11, 2011

Thanks, that surely solves my problem. I used a named function, but your code reads much better, I didn't realized that you can also modify the transition (created in mouseover function) by doing something like:

d3.select("#actor").transition().each("end", mouseover);

Very cool. I wonder if it is a good design to have the .on('mouseover') call to return the previously set mouseover handler? this works like the way of .attr() calls, sounds quite intuitive. Using this.__onmouseover is like working beneath the hood, which requires much knowledge of the implementation.

@mbostock
Copy link
Member

Yeah, it'd be nice if .on("mouseover") returned the mouseover handler.

@mbostock
Copy link
Member

Fixed in 2.0.0. @a4500fcfb112a011a59c123b000508cb326b453a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants