How to add an onclick event to a joint.js element?

后端 未结 2 1478
被撕碎了的回忆
被撕碎了的回忆 2021-02-13 19:41

I have a joint.js element in a DAG, and would like to be able to trigger an event by clicking on it.

I could use $(selector).click(...) to do it, but I was

相关标签:
2条回答
  • 2021-02-13 19:59

    The JointJS shapes are models, so you're right that click handlers won't work on them. The JointJS paper triggers events that might be useful to you:

    paper.on('cell:pointerdown', 
        function(cellView, evt, x, y) { 
            alert('cell view ' + cellView.model.id + ' was clicked'); 
        }
    );
    

    other events are: cell:pointerup, cell:pointerdblclick, cell:pointermove.

    The complete list can be found here: http://jointjs.com/api#joint.dia.Paper:events.

    EDIT:

    Starting from JointJS v0.9, there is also a cell:pointerclick event.

    0 讨论(0)
  • 2021-02-13 20:15

    You can also use Backbone itself to attach specific events to specific models. JointJS is just Backbone under the hood.

    const newElement = new jointjs.shapes.devs.Model({....});
    graph.addCell(newElement);
    
    newElement
      .findView(paper)
      .$el.find('.Settings') // this is nested in the model / cell
      .click(() => {
        // do something
      });
    
    0 讨论(0)
提交回复
热议问题