How to handle events with a long click and right-click in Backbone JS

We can manage click and double click events as shown below:

events: { "click .tree-toggler": "toggletree", "dblclick .doubleclick" : "doubleclickFunc" }, toggletree: function(e){ //code }, doubleclickFunc : function(e){ //code } 

But I want to control the right click event and the Long click event. How to handle them?

+6
source share
2 answers

I don’t know about the event with a long click (I didn’t even know that it exists and cannot find any document), but in any case. Backbone uses the jQuery on method to bind your events to the DOM. This means that everything that works with on will work with Backbone.View.events (unfortunately, there are some limitations for the selector you specify, but there is one more thing).

Try:

 events: { contextmenu: 'onRightClick' }, onRightClick: function() { alert('it works!'); } 
+12
source

You can use the contextmenu event to detect right-clicks, as described in an earlier answer. Another way to detect right clicks is with jquery event.which :

 clickTree: function(e) { if (event.which === 3) { // handle right clicks this.showtreemenu(e); return; } // handle left clicks this.toggletree(e); } 

For long clicks that also measure click duration, use mouseup and mousedown :

 events: { 'mousedown .measure-click-duration': 'clickStarted', 'mouseup .measure-click-duration': 'clickEnded' }, clickStarted: function(e) { this.clickStartTime = e.timeStamp; }, clickEnded: function(e) { var clickDuration = e.timeStamp - this.clickStarted; if (clickDuration > 1000) { this.longClick(e); } } 

I did a fiddle , demonstrating contextmenu for right clicks and the duration of the click described above.

+4
source

Source: https://habr.com/ru/post/945932/


All Articles