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

2

12

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 wondering if there was a joint.js specific way of handling it, since that would probobly be better. One event I decided was a candidate for onclick was 'batch:stop'

My code:

 var variable =  new joint.shapes.basic.Rect({
     name : label,
     id: label,
     onclick : function () {alert("hello");},
     size: { width: width, height: height },
     attrs: {
         text: { text: label, 'font-size': letterSize, 'font-family': 'monospace' },
         rect: {
             fill : fillColor, 
             width: width, height: height,
             rx: 5, ry: 5,
             stroke: '#555'
         }   
     }   
 }); 
 variable.on('batch:stop', function (element) {alert(""); toggleEvidence(element.name);});
 return variable;

How should I add an onclick event?

Trout answered 9/12, 2013 at 5:4 Comment(0)
S
27

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.

Sunderance answered 9/12, 2013 at 15:23 Comment(3)
Thanks. I was reading the wrong aspect of the event documentation, since I thought it would be related to the Element object.Trout
@Sunderance could you please this question #31794598Dunedin
I am trying to trigger a function when an html link inside the jointjs element is clicked . How can that be done ?Gehlenite
G
0

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
  });
Graziano answered 3/4, 2019 at 14:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.