As you may see in my previous post about backbone, I used a showView for rendering my views as well as keeping track of current view and closing it. One of the important things that you should consider in real world application is memory leaks. We always render the views and the next view comes and render it’s html on previous view and we think it’s gone. But it steel exist and it’s listening for it’s events and each new view that we render,takes our memory. So we need to somehow close our views before rendering the new one.
What we are going to do it something like this :
- Adding close method to views prototype
- Adding onClose method to views that have child views for closing child views
- Adding showView to our router and using it
Close Method :
Firstly somewhere in your code need to add close method. If you are using RequireJS for your project I think it’s a good idea to require backbone in our main.js file and add it .
1 2 3 4 5 6
this code add close method to all your views and when it calls it check if view have innerViews to run onClose and get rid of them and then remove the view. remove method remove the view’s element form DOM and by calling
this.stopListening() unbinds all the events it’s listening to.
onClose Method :
Now for the views that have child views, we need to push all of them to and array and make them ready to be closed when onClose called.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
Router’s ShowView :
Now we can easily add a showView function inside our outer as a method and each time for creating an instance of our view and rendering it, use this function. It should be noted that you can add some route filtering for checking for authentication or etc. to our showView but it is a simple one.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
So the showView, checks if there is any current view and if it is, it close it then render our view and assign the rendered view as app’s currentView. At the end it returns the view that in more complex application you may need it !
Note : for view rendering and use of
view.$el instead of
view.render().el read my previous backbone tip.
That’s it. Hope it would be helpful.
See ya !!!