Destruye o elimina una vista en Backbone.js


Actualmente estoy tratando de implementar un método destroy/remove para las vistas, pero no puedo obtener una solución genérica que funcione para todas mis vistas.

Esperaba que hubiera un evento para adjuntar al controlador, de modo que cuando una nueva solicitud llegue destruya las vistas anteriores y luego cargue las nuevas.

¿Hay alguna manera de hacer esto sin tener que construir una función de eliminación para cada vista?

Author: nietonfir, 2011-07-04

7 answers

Sin conocer toda la información... Puede vincular un disparador de reinicio a su modelo o controlador:

this.bind("reset", this.updateView);

Y cuando desee restablecer las vistas, active un restablecimiento.

Para su devolución de llamada, haga algo como:

updateView: function() {
  view.remove();
  view.render();
};
 47
Author: joshvermaire,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-07-08 06:48:52

Tenía que estar absolutamente seguro de que la vista no solo se eliminó de DOM, sino que también se liberó completamente de los eventos.

destroy_view: function() {

    // COMPLETELY UNBIND THE VIEW
    this.undelegateEvents();

    this.$el.removeData().unbind(); 

    // Remove view from DOM
    this.remove();  
    Backbone.View.prototype.remove.call(this);

}

Me pareció exagerado, pero otros enfoques no funcionaron completamente.

 160
Author: sdailey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-05-23 01:41:49

Sé que llego tarde a la fiesta, pero espero que esto sea útil para alguien más. Si está utilizando backbone v0. 9. 9+, podría usar, listenTo y stopListening

initialize: function () {
    this.listenTo(this.model, 'change', this.render);
    this.listenTo(this.model, 'destroy', this.remove);
}

stopListening se llama automáticamente por remove. Usted puede leer más aquí y aquí

 20
Author: Bassam Mehanni,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-14 04:11:48

Esto es lo que he estado usando. No he visto ningún problema.

destroy: function(){
  this.remove();
  this.unbind();
}
 8
Author: JT703,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-14 20:52:17

De acuerdo con la documentación actual de Backbone....

Vista.remove ()

Elimina una vista y su el del DOM, y llama a stopListening para eliminar cualquier evento enlazado que la vista haya escuchado.

 4
Author: Dre,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-02-11 03:46:41

Creo que esto debería funcionar

destroyView : function () {
    this.$el.remove();
}
 0
Author: Chhorn Ponleu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-12 08:56:27

¡Podrías usar la forma de resolver el problema!

initialize:function(){
    this.trigger('remove-compnents-cart');
    var _this = this;
    Backbone.View.prototype.on('remove-compnents-cart',function(){
        //Backbone.View.prototype.remove;
        Backbone.View.prototype.off();
        _this.undelegateEvents();
    })
}

Otra forma:Crear una variable global, como esta: _global.routerList

initialize:function(){
    this.routerName = 'home';
    _global.routerList.push(this);
}
/*remove it in memory*/
for (var i=0;i<_global.routerList.length;i++){
    Backbone.View.prototype.remove.call(_global.routerList[i]);
}
 0
Author: Deot,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-25 07:45:06