So i have some data binded with drag event listeners :
JavaScript
x
8
1
myNodes.enter()
2
.append("svg:g")
3
.call(d3.behavior.drag()
4
.on("drag", function() {
5
console.log(d3.event.dx, d3.event.dy);
6
})
7
);
8
Now I want to call this onDrag function on a certain node programmatically. I do know the same is possible with standard events by doing
JavaScript
1
3
1
aNode.on("click")() // works
2
aNode.on("drag")() // doesn't work
3
Is there any way to do so ? Thanks.
Advertisement
Answer
Save the callback (the one you pass into the drag handler) in a variable and then call this variable in your other context.
JavaScript
1
11
11
1
var dragCallback = function(){
2
console.log(d3.event.dx, d3.event.dy);
3
};
4
var dragBehavior = d3.behavior.drag()
5
.on("drag", dragCallback);
6
myNodes.enter()
7
.append("g")
8
.call(dragBehavior);
9
//Call drag method programmatically
10
dragCallback()
11