admin管理员组

文章数量:1335138


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


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});
Share Improve this question edited Mar 11, 2013 at 6:40 cclerv 2,9698 gold badges37 silver badges43 bronze badges asked Mar 11, 2013 at 6:34 bitsbufferbitsbuffer 5551 gold badge7 silver badges17 bronze badges 6
  • Can you please explain what do you mean by new position ? How do you calculate it ? – Sachin Jain Commented Mar 11, 2013 at 6:52
  • myView object is put under a jquery widget.After double click the widget size changes along with its offset. I'm taking its new position and dimensions.Please note that I dont have any control on widget. – bitsbuffer Commented Mar 11, 2013 at 7:45
  • So jquery widget is giving you the new dimensions and size.Right ? – Sachin Jain Commented Mar 11, 2013 at 7:49
  • My view is inside the jquery widget so yes jquery widget is giving me new dimensions – bitsbuffer Commented Mar 11, 2013 at 8:53
  • Doesn't below answers meet your expectation ? – Sachin Jain Commented Mar 11, 2013 at 11:21
 |  Show 1 more ment

3 Answers 3

Reset to default 4

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

}

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.

You can pass widget in view itself, then you will have full control over widget.

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

本文标签: javascriptPassing Data to event handler in Backbone ViewStack Overflow