Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing Data to event handler in Backbone View


I'm developing a web app using Backbonejs. I have a use case where I have to pass the new position of div1 to a double click event handler of a 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});
like image 390
bitsbuffer Avatar asked Feb 18 '23 05:02

bitsbuffer


2 Answers

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

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

}
like image 53
Ulug'bek Avatar answered Mar 03 '23 20:03

Ulug'bek


Assuming that your view element is a child element of the jquery widget, the best thing is probably to grab the values you need 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'll need to replace .widget-selector with a selector that will work for the jquery widget.

like image 25
Ben Avatar answered Mar 03 '23 20:03

Ben