When you use the events object in the Backbone view, they are linked using the jQuery delegate :
delegateEvents delegateEvents([events])
Uses the jQuery delegate function to provide declarative callbacks for DOM events in a view. If the events hash is not passed directly, this.events used as the source.
Thus, only those elements that are in the this.el will be linked using the events view. You say that
The input element is not part of the bookmarksList element
therefore, nothing is bound to input.searchBookmark , and your search method will never be called.
You have several options:
- Move the search box to
#bookmarksList so that the list is offline. - Move search event processing to a view containing a search box. Then set up a separate set of bookmarks for
#bookmarksList to display and update the display when the collection changes. Then, the view in the search box can filter the main collection of bookmarks, update the collection that #bookmarksList uses, and let Backbone handle events from there. - Manually bind to
input.searchBookmark when your view #bookmarksList displayed and untied in the remove method.
The first two are pretty standard Backbone settings, so don't talk about them anymore; the third is a little strange and will look something like this:
window.BookmarksListView = Backbone.View.extend({ events: { }, initialize: function() { _.bindAll(this, 'search'); //... }, render: function(eventName) { $('input.searchBookmark').on('keyup', this.search); //... }, remove: function() { $('input.searchBookmark').off('keyup', this.search); // Other cleanup... }, //... });
I do not recommend this approach, but your views should hold them in your hands.
source share