简体   繁体   English

如何将onclick事件添加到joint.js元素?

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

I have a joint.js element in a DAG, and would like to be able to trigger an event by clicking on it. 我在DAG中有一个joint.js元素,并希望能够通过单击它来触发事件。

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. 我可以使用$(selector).click(...)来做它,但我想知道是否有一个joint.js特定的处理方式,因为这可能会更好。 One event I decided was a candidate for onclick was 'batch:stop' 我决定的一个事件是onclick的候选人是'批处理:停止'

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? 我应该如何添加onclick事件?

The JointJS shapes are models, so you're right that click handlers won't work on them. JointJS形状是模型,因此您点击处理程序无法使用它们是正确的。 The JointJS paper triggers events that might be useful to you: JointJS论文触发了可能对您有用的事件:

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. 其他事件有:cell:pointerup,cell:pointerdblclick,cell:pointermove。

The complete list can be found here: http://jointjs.com/api#joint.dia.Paper:events . 完整列表可以在这里找到: http//jointjs.com/api#joint.dia.Paper : events

EDIT : 编辑

Starting from JointJS v0.9, there is also a cell:pointerclick event. 从JointJS v0.9开始,还有一个cell:pointerclick事件。

You can also use Backbone itself to attach specific events to specific models. 您还可以使用Backbone本身将特定事件附加到特定模型。 JointJS is just Backbone under the hood. JointJS只是引擎盖下的Backbone。

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
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM