¿Acción persistente al enrutar en Backbone?

I have a number of parts of my web application routed in Backbone. All rendered items are inside the main body in the html, and there are a few items in the header that change depending on where the user is navigating to (or from).

This is all fine, however, controlling these dynamic changes requires some redundant code. Is it possible to add an action when routing that always triggers? A use case for this, for example, could be pushing a loading icon to the screen between renders.

Not looking for a default action (when no route is define), but a persistent action that triggers with every new navigation.

Gracias de antemano!

preguntado el 03 de mayo de 12 a las 15:05

1 Respuestas

You could bind every route change in the router to a function. Just add this in the initializer/constructor method of your Router:

initialize: function() {
  this.on('all', this.routeChanged, this)

routeChanged: function(route) {
  // perform repeating tasks here

contestado el 03 de mayo de 12 a las 16:05

Ah, that's exactly what I was looking for! Didn't put the two and two together, but of course, event binders in Backbone are available throughout the MVC, right? Thanks! - Yann Evas

You should award people that have helped you upvoting and/or accepting their answers. - fguillén

I think it is worth mentioning that the "all" event is a general feature of Backbone.Events (documentcloud.github.com/backbone/#Events-on) so it works with all event-ish things in Backbone. - mu es demasiado corto

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.