This is a direct copy of this file on GitHub. I’m reproducing here because I think it is a great checklist that actually solved my problem when my view events were not firing.
I’ve been using backbone.js to > develop some javascript-heavy application recently.
I find it quite common for new comers to come into the following > problem: why ain’t my view events firing?
By “view events” I mean a events hash like the following in a > Backbone View class declaration.
class Mine.Views.TasksNew extends Backbone.View events: "click #preview_button":"parse" constructor:(options)-> super(options) parse:(e)-> alert 'clicked' ...
When your events are not firing, the following is a useful > checklist to go through.
Maybe you have in the previous class declaration something like “el:\$(‘#myelement’)” and expecting the View to be bound to that specific element. But be cautious! The class declaration may be executed before the page is fully loaded. (This is common since I have seen most people put the class declaration before everything else). Please try to move “el:\$(‘#myelement’)” to the spot where you instantiate the View class and maybe the problem could be solved.
Maybe your “el” is too restrictive. You should be know that the objects mentioned in the events hash are restricted inside the el element.
Be aware of the order inside
"click #preview_button":"parse"
! It’sevent
followed byselector
followed byhandler
. Did you reversed the order?- Trace down the code using firebug,etc. The code of backbone.js is well readable.
This checklist incorporated the bad things that has happened to me > that makes my events not firing. Hope it helps you!
My specific problem was solved after hitting the second item on the checklist. “el” was too restrictive. I had el: '#posts'
, so my events were being restricted to within the #posts
div. I changed the el
to el: '#posts'
, and that solved my problem of the events not being fired. I’m sure this checklist would come in handy one day.