The difference between turning on / off the trunk (separation) and the function of listening / stopping playback

I am currently using the framework to implement my application. As part of memory management, I will run breaks of all views when switching views

teardown: -> for viewName, view of @subViews view.teardown() for object, events of @objectEvents @_handleObjectEvents("off", object, events) @off() @remove() @undelegateEvents() @ 

How sufficient is this approach to ensure that most memory problems are resolved? The task that I see here is that I need to track all the subroutines of each type and cause gaps for all the main types and subzones as part of the cleanup.

I did a few searches and found that this trunk also has these two events: "listenTo" and "stopListening", where we control the binding of events to models at the presentation level.

 view.listenTo(model, 'change', view.render); view.stopListening(model); 

My questions are: is there a match between my implementation of the conversation and the use of "stopListening"? Can I just use "stopListening" to manage memory?

+4
source share
2 answers

The short answer is yes, there is overlap.

A more complex answer is that the listenTo / stopListening methods introduced in Backbone 0.9.9 already use the on / off methods, but with some useful addition - they store the current event listeners in an internal object called _listeners.

The advantage of using this object is that you always know the complete list of all your listeners - you can iterate over it and remove certain elements from it (remember that the listener is just a function, and a function is just an object).

So you can call it this:

 this.stopListening(emitting_object, ["reset", "add"]) // Removes listeners for "reset" and "add" on emitting_object this.stopListening(emitting_object) // Removes all listeners on emitting_object this.stopListening() // Iterates over _listeners object and removes all listeners (probably the most usable case) 

So, using this method, you can convert your teardown method to the following:

 this.teardown = function(){ this.stopListening(); ... } 
+4
source

I would recommend using the listenTo method. Its subtlety is that when you use the remove method on your view, it automatically unties (calls stopListening) what it listens to. According to Derrick Bailey, he also untie events under the property of events.

What I will do, as I am in the process of upgrading my application to 0.9.9 from 0.9.2 (which actually still works so far), just switches all my ons / offs to listenTo and stopListening. I also basically have close methods. However, I will still call undelegateEvents, just in case. It doesn’t hurt to know that you are still getting rid of listening to events.

+3
source

All Articles