backbone.js - Not able to remove the variables declared inside backbone view -
i have read many posts issue of multiple instances of same backbone view being instantiated every time , view hangs around in dom after it's not used more, , how fix using this.remove() , this.unbind()
but how remove variables declared inside view, so:
var myview = backbone.view.extend({ el : '#somediv', var1 : '', var2 : '', array1 : [], initialize : function() { //init code here }, render : function() { //rendering code here } }); so question is, how remove instances of variables declared there: var1, var2, array1. have call view every time click on button. , every time see previous values of these variables still there. this.remove() , this.unbind() might remove view dom , undelegate events bindings.
the properties define inside backbone.view.extend call attached prototype , shared instances of view (i.e. they're sort of class properties rather instance properties). should fine var1 , var2 you'd assigning new values per-instance; array1 array , similar properties can problematic though; suppose this:
var v = new myview; v.array1.push('pancakes'); creating new instance won't deep-copy out of prototype v.array1 refer array in prototype. means next new myview have 'pancakes'.
the usual solution initialize instance properties in constructor. backbone, constructor initialize:
var myview = backbone.view.extend({ el: '#somediv', initialize: function() { this.var1 = ''; this.var2 = ''; this.array1 = [ ]; }, //... }); you can run problems el: '#somediv' uniquely identifies single dom element. long you're removing , recreating element should okay; i'd recommend letting view create , destroy own el though, run fewer zombies , leaks way.
Comments
Post a Comment