I am new to Backbone and would like some help with best practices. Here is a simplified version of my question:
I have several buttons in a form, each of which has its own Backbone Model and View. On Click, I would like some input field (outside the scope of these buttons) to be populated with that button's value.
I want to write this in a reusable way, so I would rather not define an event handler which simply calls $("input").val(...);
. I am also not a huge fan of firing/listening for custom events.
The solution I have developed is passing a function in the options hash which is ultimately called by the View's event handler. Is there a better way? thanks!
delegateEvents
in theButtonsListView
, attached a click handler to that class. The handler just reads adata-
attribute on the button to determine the value to populate theinput
with. The avantage ofdelegateEvents
inrender()
is that you can properly clean the event handlers – Bruno Grieder