anvaka / VivaGraphJS

Graph drawing library for JavaScript
Other
3.73k stars 423 forks source link

exception on registering events to the renderer #134

Closed ahmetkizilay closed 9 years ago

ahmetkizilay commented 9 years ago

I am trying to register an event callback with renderer.on('scale', fn) but vivagraph (0.7.8) throws an exception.

the error is that inside the on method publicEvents.addEventListener is not a defined function. I checked with the inspector and publicEvents does not have that function indeed. In fact, it looks like there is a wrong assignment here for publicEvents, because it seems to contain methods that belong to the graph module.

see the js fiddle here: http://jsfiddle.net/sym6b55k/

public_events

ahmetkizilay commented 9 years ago

and patching the code like this seems to work:

    on: function(eventName, callback) {
      // publicEvents.addEventListener(eventName, callback);
      publicEvents.on(eventName, callback);
      return this;
    },

    off: function(eventName, callback) {
      // publicEvents.removeEventListener(eventName, callback);
      publicEvents.off(eventName, callback);
      return this;
    }
ahmetkizilay commented 9 years ago

in addition to that fix, we also need to change the following line on renderer.js:

var eventify = require('ngraph.graph');

it should be

var eventify = require('ngraph.events');

https://github.com/anvaka/VivaGraphJS/blob/master/src/View/renderer.js#L9

after this fix, publicEvents object has only three methods on, off, and fire, which makes more sense.

anvaka commented 9 years ago

Thanks for reporting this and fixing! Do you want to send the pull request?

anvaka commented 9 years ago

Thank you once again for the PR. This is merged and released as v0.7.9

gg4u commented 5 years ago

@ahmetkizilay Could you please explain how eventify worked in your example ?

I tried to registered renderer.on('scroll', fn) But nothing happened.

In your example I tried to see what is passed to the function:

renderer.on('scale', function(e) { console.log(e, renderer) })

it prints the scale value but cannot see where does it take that property from renderer.

My intention is to explore if I can handle events better in SVG, and use only one listener to the renderer / graph container.