The launch of Raphael events from the outside

My application uses Raphaël to remove a collection of objects from a page, each with a handler binding clickthat uses data attached to the object when it was loaded via JSON. Everything is working fine.

Now I'm trying to add to some test coverage using Cucumber (yes, I know that I had to build tests first, I will do it next time). I need to call a click on the first object to verify that these objects are related to page loading. Ive tried to find the appropriate SVG path for this object and trigger an event clickagainst it, but this does not work. I also tried dropping each Raphaël set into a globally accessible object, but I could not determine how to raise the Raphaël event clickagainst the corresponding one.

To ask some specific questions:

1) How to trigger the Raphaël event manually?

2) Is it possible to trigger the mentioned event if you have a link to the SVG element owned by Raphaël?

3) If not, is it possible to access the current set of sets that Raphael contains?

+5
source share
2 answers

I found that the first discovery of the raphael object and then the intersection of the DOM allowed me to find the functions of my event handlers. For me, the click event was the 0th event.

So, to invoke everything I did, this is:

 raphobj.events[0].f();

Be careful, because if in the click event you refer to 'this', this will not work.

+4
source

You 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");
+8
source

All Articles