0

What is the best practice for handling links in a Backbone app?

a) Should each view listen to a click event for links rendered by itself? b) Or should there be a global click event listener attached to, say, document, that intercepts all link clicks and executes Backbone.History.navigate with their href?

I have seen both approaches, is there a preferred one?

2
  • if under 'handle links' you mean navigation between pages - Backbone Router should maintain this, if you mean listen click events - View should listen to events inside its template
    – Evgeniy
    Nov 21, 2014 at 11:50
  • Correct me if I am wrong. But Backbone.Router only handles links (navigates) with '#'. With regular links (using pushState), you need to intercept a click , point the router to the href and cancel the default action. All manually, as far as I know.
    – Yaro
    Nov 22, 2014 at 11:22

1 Answer 1

0

I think this depends on the application you are trying to write.

What I generally do is, if it's a list of items where the count is greater than 10 and each one of these items have click handlers, I put the click handler on the parent view or document view. If you only need 1 instance of the view with the click handler, then you needn't worry about delegating your events to the document/parent.

1
  • For other actions - sure. But what about the navigation links around the app, like 'Back', 'Home', etc..?
    – Yaro
    Nov 22, 2014 at 11:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.