0

There is a 'View' in the model with the event click. After using the Quicksand effects plug-in for jQuery, the objects loose their event handlers. I have tried to add the listener for the event with standard methods in backbone.js:

events: {
    "click .objContact"    : "openChat"
}

and the same tools jQuery delegate:

var self=this; 
this.$el.delegate('.objContact','click', function(){
    self.openChat();
});

and live:

var self=this; 
this.$el.find('.objContact').live('click', function(){
    self.openChat();
});

but the click event disappears. What could be the problem? And how do I solve it?

UPD: Calling 'Quicksand' is in Backbone.Router (subject to change is obtained directly by means of jQuery, not Backbone), so changes are not handled in Backbone.View

UPD 2: The problem is solved in the following way - by moving the handling of the click event from the View-model to View-collection. And treated with live (did not work in on)

S. Albano
  • 707
  • 7
  • 21

2 Answers2

2

Simple Answer: instead of linking the function to the link with the classic ajax method that is

$('a.oldJqueryClass').click(function(){....

you need to make that function standalone, declaring a new function

function myfunction(params) {alert(params);}

than in the link you call that with the old school way:

<a href="#" class="oldJqueryClass" onclick="javascript:myfunction('My alert message')">Click here</a>

In this way the cloned element will contain itself the call to the function and you can forget about restoring the dom integrity broken by the cloning of quicksand.

I did it in my project, it works fine.

  • Yes, this option is also good. But in my case, the code is executed in an extension for Chrome and the introduction of new rules 'Manifest 2' it - is prohibited. – Dima Kodnik Oct 13 '12 at 12:15
1

Do a call to delegateEvents() after the related DOM entries have changed or become overwritten. In a traditional Backbone app this is typically done in the render method, but you probably need for figure out when and where quicksand does it's magic (I do not know anything about it), and call delegateEvents that will reactivate the events for the current elements in the DOM.

Marius Kjeldahl
  • 6,830
  • 3
  • 33
  • 37
  • You might need to [`setElement`](http://backbonejs.org/#View-setElement) instead of just `delegateEvents` depending on what quicksand does to the DOM. – mu is too short Sep 08 '12 at 22:17
  • In _QuickSand_ have such a line `var $dest = $($sourceParent).clone()`. But the idea (and documentation) `.live` was to create an event for the newly created item, but did not work. – Dima Kodnik Sep 09 '12 at 08:31
  • Clone only clones an element, but does not insert it into the DOM AFAIK. How it is inserted into the DOM may affect events and even the element attached to the view itself as mu points out. Theoretically, cloning and element with a (supposedly unique) id may also be a source for confusion. – Marius Kjeldahl Sep 09 '12 at 11:41