gjunkie
gjunkie

Reputation: 828

Backbone.js - Destroying a Marionette Controller

I'm having issues destroying a Marionette controller. I'm still wrapping my brains around Marionette, and more specifically Backbone garbage collection....

This controller instantiates several views, each of which may have several bound event listeners. The code looks something like this:

myController.js
---------------
Marionette = require('backbone.marionette');
MyView     = require('path/to/myView');

var MyController = Marionette.Controller.extend({
    initialize: function(options) {
        console.log('init');
    }
    onDestroy: function() {
        console.log('should be destroyed');
    }
    showData: function() {
        console.log('create view');
        myView = new MyView();
    }
});



appController.js
----------------
MyController = require('path/to/myController');

var controller = new MyController()
controller.showData();
controller.destroy();


output:
//init
//create view
//should be destroyed

Controller is still around, even though onDestroy does fire..

Could this be an issue with views still having bound event listeners? My apologies if the code looks weird... translating this from coffeescript.

Upvotes: 0

Views: 641

Answers (1)

Trace
Trace

Reputation: 18859

To call the destroy method, the Marionette controller has a "close" method defined on its prototype:

controller.close(); 

The method that is called is onClose.

For info, you can check this in the console:

enter image description here

Upvotes: 1

Related Questions