admin管理员组文章数量:1303132
My app is using Raphaël to drop a collection of objects onto a page, each with a click
handler bound that uses data attached to the object when it was loaded in via JSON. That’s all working fine.
I now am trying to add in some test coverage using Cucumber (yes, I know I should have built the tests in first, I will do next time). I need to trigger a click on the first object to test that that object’s associated page loads. I’ve tried finding the appropriate SVG path for that object and triggering a click
event against it, but that doesn’t work. I also tried dropping each Raphaël set into a globally available object but couldn’t work out how to trigger a Raphaël click
event against the appropriate one.
To give some specific questions:
1) How would one trigger a Raphaël event manually?
2) Is it possible trigger said event if you have a reference to the SVG element the Raphaël set owns?
3) If not, is it possible to access the current collection of sets Raphaël is holding?
My app is using Raphaël to drop a collection of objects onto a page, each with a click
handler bound that uses data attached to the object when it was loaded in via JSON. That’s all working fine.
I now am trying to add in some test coverage using Cucumber (yes, I know I should have built the tests in first, I will do next time). I need to trigger a click on the first object to test that that object’s associated page loads. I’ve tried finding the appropriate SVG path for that object and triggering a click
event against it, but that doesn’t work. I also tried dropping each Raphaël set into a globally available object but couldn’t work out how to trigger a Raphaël click
event against the appropriate one.
To give some specific questions:
1) How would one trigger a Raphaël event manually?
2) Is it possible trigger said event if you have a reference to the SVG element the Raphaël set owns?
3) If not, is it possible to access the current collection of sets Raphaël is holding?
Share Improve this question edited Sep 8, 2020 at 8:34 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Aug 21, 2012 at 16:55 Robin WhittletonRobin Whittleton 6,3394 gold badges42 silver badges67 bronze badges2 Answers
Reset to default 8You can add this custom method to your raphael elements:
Raphael.el.trigger = function(eventName){
for(var i = 0, len = this.events.length; i < len; i++) {
if (this.events[i].name == eventName) {
this.events[i].f.call(this);
}
}
}
You can use it just like:
var r = paper.rect(0,0,100,100).attr({"fill": "#f00"});
r.click(function(){
this.attr("fill": "#0f0");
});
r.trigger("click");
I found that first finding the raphael object, then traversing the DOM allowed me to find the functions of their event handlers. For mine, the click event was the 0th event.
So to trigger it all I did was:
raphobj.events[0].f();
Be careful though, because if in the click event you reference 'this' it won't work.
本文标签: javascriptTriggering Raphael events externallyStack Overflow
版权声明:本文标题:javascript - Triggering Raphael events externally - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741748657a2395690.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论