Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7887865
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:41:28+00:00 2026-06-03T05:41:28+00:00

in backbone we have an app that uses an event Aggregator, located on the

  • 0

in backbone we have an app that uses an event Aggregator, located on the window.App.Events
now, in many views, we bind to that aggregator, and i manually wrote a destroy function on a view, which handles unbinding from that event aggregator and then removing the view. (instead of directly removing the view).

now, there were certain models where we needed this functionality as well, but i can’t figure out how to tackle it.

certain models need to bind to certain events, but maybe i’m mistaken but if we delete a model from a collection it stays in memory due to these bindings to the event aggregator which are still in place.

there isn’t really a remove function on a model, like a view has.
so how would i tacke this?

EDIT
on request, some code example.

App = {
    Events: _.extend({}, Backbone.Events)
};

var User = Backbone.Model.extend({

    initialize: function(){
        _.bindAll(this, 'hide');
        App.Events.bind('burglar-enters-the-building', this.hide);
    },

    hide: function(burglarName){
        this.set({'isHidden': true});
        console.warn("%s is hiding... because %s entered the house", this.get('name'), burglarName);
    }

});

var Users = Backbone.Collection.extend({

    model: User

});

var House = Backbone.Model.extend({

    initialize: function(){
        this.set({'inhabitants': new Users()});
    },

    evacuate: function(){
        this.get('inhabitants').reset();
    }

});



$(function(){

    var myHouse = new House({});

    myHouse.get('inhabitants').reset([{id: 1, name: 'John'}, {id: 1, name: 'Jane'}]);

    console.log('currently living in the house: ', myHouse.get('inhabitants').toJSON());

    App.Events.trigger('burglar-enters-the-building', 'burglar1');

    myHouse.evacuate();

    console.log('currently living in the house: ', myHouse.get('inhabitants').toJSON());

    App.Events.trigger('burglar-enters-the-building', 'burglar2');

});​

view this code in action on jsFiddle (output in the console): http://jsfiddle.net/saelfaer/szvFY/1/

as you can see, i don’t bind to the events on the model, but to an event aggregator.
unbinding events from the model itself, is not necessary because if it’s removed nobody will ever trigger an event on it again. but the eventAggregator is always in place, for the ease of passing events through the entire app.

the code example shows, that even when they are removed from the collection, they don’t live in the house anymore, but still execute the hide command when a burglar enters the house.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T05:41:29+00:00Added an answer on June 3, 2026 at 5:41 am

    I see that even when the binding event direction is this way Object1 -> listening -> Object2 it has to be removed in order to Object1 lost any alive reference.

    And seeing that listening to the Model remove event is not a solution due it is not called in a Collection.reset() call then we have two solutions:

    1. Overwrite normal Collection cleanUp

    As @dira sais here you can overwrite Collection._removeReference to make a more proper cleaning of the method.

    I don’t like this solutions for two reasons:

    • I don’t like to overwrite a method that has to call super after it.
    • I don’t like to overwrite private methods

    2. Over-wrapping your Collection.reset() calls

    Wich is the opposite: instead of adding deeper functionality, add upper functionality.

    Then instead of calling Collection.reset() directly you can call an implementation that cleanUp the models before been silently removed:

    cleanUp: function( data ){
      this.each( function( model ) { model.unlink(); } );
      this.reset( data );
    } 
    

    A sorter version of your code can looks like this:

    AppEvents = {};
    _.extend(AppEvents, Backbone.Events)
    
    var User = Backbone.Model.extend({
      initialize: function(){
        AppEvents.on('my_event', this.listen, this);
      },
    
      listen: function(){
        console.log("%s still listening...", this.get('name'));
      },
    
      unlink: function(){
       AppEvents.off( null, null, this );
      }
    });
    
    var Users = Backbone.Collection.extend({
      model: User,
    
      cleanUp: function( data ){
        this.each( function( model ) { model.unlink(); } );
        this.reset( data );
      }
    });
    
    
    // testing
    var users = new Users([{name: 'John'}]);
    console.log('users.size: ', users.size()); // 1
    AppEvents.trigger('my_event');             // John still listening...
    
    users.cleanUp();
    console.log('users.size: ', users.size()); // 0
    AppEvents.trigger('my_event');             // (nothing)
    

    Check the jsFiddle.

    Update: Verification that the Model is removed after remove the binding-event link

    First thing we verify that Object1 listening to an event in Object2 creates a link in the direction Obect2 -> Object1:

    Our object is retained

    In the above image we see as the Model (@314019) is not only retained by the users collection but also for the AppEvents object which is observing. Looks like the event linking for a programmer perspective is Object that listen -> to -> Object that is listened but in fact is completely the opposite: Object that is listened -> to -> Object that is listening.

    Now if we use the Collection.reset() to empty the Collection we see as the users link has been removed but the AppEvents link remains:

    Our object is retained 2

    The users link has disappear and also the link OurModel.collection what I think is part of the Collection._removeReference() job.

    When we use our Collection.cleanUp() method the object disappear from the memory, I can’t make the Chrome.profile tool to explicitly telling me the object @314019 has been removed but I can see that it is not anymore among the memory objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Rails 3.1 app that uses the codebrew/backbone-rails . In a .jst.ejs
I have some Backbone.js code that bind a click event to a button, and
We have a backbone.js app that displays a number of forms to the user.
I have a backbone app with a view structure that looks like the following
I hope you are all well! A question for an app that uses Backbone.js
I'm having some difficulty with backbone js. Currently, I have a rails app that
I have a backbone.js app ( www.github.com/juggy/job-board ) where I want to bind my
I have a Backbone View that uses iScroll to implement a slideshow. iScroll publishes
I have a web app that I want to use Backbone.js for. I have
I have the following backbone.js code and i have a problem in that event

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.