Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js : repopulate or recreate the view?

In my web application, I have a user list in a table on the left, and a user detail pane on the right. When the admin clicks a user in the table, its details should be displayed on the right.

I have a UserListView and UserRowView on the left, and a UserDetailView on the right. Things kind of work, but I have a weird behavior. If I click some users on the left, then click delete on one of them, I get successive javascript confirm boxes for all users that have been displayed.

It looks like event bindings of all previously displayed views have not been removed, which seems to be normal. I should not do a new UserDetailView every time on UserRowView? Should I maintain a view and change its reference model? Should I keep track of the current view and remove it before creating a new one? I'm kind of lost and any idea will be welcome. Thank you !

Here is the code of the left view (row display, click event, right view creation)

window.UserRowView = Backbone.View.extend({     tagName : "tr",     events : {         "click" : "click",     },     render : function() {         $(this.el).html(ich.bbViewUserTr(this.model.toJSON()));         return this;     },     click : function() {         var view = new UserDetailView({model:this.model})         view.render()     } }) 

And the code for right view (delete button)

window.UserDetailView = Backbone.View.extend({     el : $("#bbBoxUserDetail"),     events : {         "click .delete" : "deleteUser"     },     initialize : function() {         this.model.bind('destroy', function(){this.el.hide()}, this);     },     render : function() {         this.el.html(ich.bbViewUserDetail(this.model.toJSON()));         this.el.show();     },     deleteUser : function() {         if (confirm("Really delete user " + this.model.get("login") + "?"))              this.model.destroy();         return false;     } }) 
like image 437
solendil Avatar asked Sep 27 '11 10:09

solendil


People also ask

Do people still use Backbone JS?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.

How does Backbone JS work?

BackboneJS provides various building blocks such as models, views, events, routers and collections for assembling the client side web applications. When a model changes, it automatically updates the HTML of your application. BackboneJS is a simple library that helps in separating business and user interface logic.

What is backbone JS model?

Model contains dynamic data and its logic. It performs various types of action on the data like validation, conversion, computed properties, access control etc. 1. It extends Backbone.


1 Answers

I always destroy and create views because as my single page app gets bigger and bigger, keeping unused live views in memory just so that I can re-use them would become difficult to maintain.

Here's a simplified version of a technique that I use to clean-up my Views to avoid memory leaks.

I first create a BaseView that all of my views inherit from. The basic idea is that my View will keep a reference to all of the events to which it's subscribed to, so that when it's time to dispose the View, all of those bindings will automatically be unbound. Here's an example implementation of my BaseView:

var BaseView = function (options) {      this.bindings = [];     Backbone.View.apply(this, [options]); };  _.extend(BaseView.prototype, Backbone.View.prototype, {      bindTo: function (model, ev, callback) {          model.bind(ev, callback, this);         this.bindings.push({ model: model, ev: ev, callback: callback });     },      unbindFromAll: function () {         _.each(this.bindings, function (binding) {             binding.model.unbind(binding.ev, binding.callback);         });         this.bindings = [];     },      dispose: function () {         this.unbindFromAll(); // Will unbind all events this view has bound to         this.unbind();        // This will unbind all listeners to events from                                // this view. This is probably not necessary                                // because this view will be garbage collected.         this.remove(); // Uses the default Backbone.View.remove() method which                        // removes this.el from the DOM and removes DOM events.     }  });  BaseView.extend = Backbone.View.extend; 

Whenever a View needs to bind to an event on a model or collection, I would use the bindTo method. For example:

var SampleView = BaseView.extend({      initialize: function(){         this.bindTo(this.model, 'change', this.render);         this.bindTo(this.collection, 'reset', this.doSomething);     } }); 

Whenever I remove a view, I just call the dispose method which will clean everything up automatically:

var sampleView = new SampleView({model: some_model, collection: some_collection}); sampleView.dispose(); 

I shared this technique with the folks who are writing the "Backbone.js on Rails" ebook and I believe this is the technique that they've adopted for the book.

Update: 2014-03-24

As of Backone 0.9.9, listenTo and stopListening were added to Events using the same bindTo and unbindFromAll techniques shown above. Also, View.remove calls stopListening automatically, so binding and unbinding is as easy as this now:

var SampleView = BaseView.extend({      initialize: function(){         this.listenTo(this.model, 'change', this.render);     } });  var sampleView = new SampleView({model: some_model}); sampleView.remove(); 
like image 196
Johnny Oshika Avatar answered Sep 29 '22 05:09

Johnny Oshika