Passing data to an event handler in Backbone View mode


I am developing a web application using Backbonejs. I have a use case when I need to pass a new position div1 to the double-click event handler in the Backbone view.

My code looks like

var MyView = Backbone.Views.extend({ events: { 'dblclick #div1' : 'div1ClickHandler' //here I want to pass new offset for #div1 } }); div1ClickHandler: function() { ...... } var myView = new MyView({model: myModel,el : #div1}); 
+4
source share
3 answers

Assuming your view element is a child of a jquery widget, it’s best to probably capture the values ​​you want in the click handler:

 var MyView = Backbone.Views.extend({ events: { 'dblclick #div1' : 'div1ClickHandler' } }); div1ClickHandler: function() { var $this = $(this); var $widget = $this.parents('.widget-selector:first'); $this.offset($widget.offset()); $this.height($widget.height()); $this.width($widget.width()); } var myView = new MyView({model: myModel,el : #div1}); 

If the jquery widget is always the direct parent of your view element, you can replace parents('.widget-selector:first') with parent() ; otherwise, you will need to replace the .widget-selector selector that will work for the jQuery widget.

+2
source

You can do this: inside the div you need to add a new field called data-yourfieldName and from a js call that:

 yourFunctionName: function(e) { e.preventDefault(); var email = $(e.currentTarget).data("yourfieldName"); } 
+4
source

You can transfer the widget in view mode, then you will have full control over the widgets.

 var MyView = Backbone.Views.extend({ initialize: function(options) { this.widget = options.widget; // You will get widget here which you passed at the time of view creation } events: { 'dblclick #div1' : 'div1ClickHandler' //here I want to pass new offset for #div1 } }); div1ClickHandler: function() { // Query to fetch new position and dimensions using widget // update the respective element } var myView = new MyView({model: myModel, el: $('#div1'), widget: widgetInstance}); 
+2
source

All Articles