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 8338443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:35:35+00:00 2026-06-09T04:35:35+00:00

This time I’m struggling with the different methods to bind events. I’ve all the

  • 0

This time I’m struggling with the different methods to bind events. I’ve all the mentioned methods in my code. I just don’t know, if I’m on the right way. Maybe I should always use bindTo to ensure that my views are closed completely after a change (at present this would often yield errors)? Are there any best practices which will help me to get in the right direction?

To illustrate my current understanding of Marionette, here’s one module from my app. As always, every hint is very welcome.

PP.module('Grid', function(Grid, PP, Backbone, Marionette, $, _){

  Grid.Product = Backbone.Model.extend({});

  Grid.ProductCollection = Backbone.Collection.extend({
    model: Grid.Product,
    url: '/products/query'
  });

  Grid.ProductView = Backbone.Marionette.ItemView.extend({
    className: 'grid',
    template: 'public/templates/grid-template'
  });

  // Helper Methods
  // -----------------------

  var getGenderCode = function(genderName){
    var genderMap = {
      'men': 'M',
      'women': 'W',
      'unisex': 'A'
    }

    return genderMap.genderName;
  }

  // Public API
  // -------------------

  Grid.renderGrid = function(productCollection){
    Grid.productView = new Grid.ProductView({
      collection: productCollection
    });

    Grid.productView.bind('show', function(){
      $('#isotope-container').isotope({
        itemSelector : '.item',
        containerStyle: {
          position: 'relative',
          zIndex: 1
        }
      });
    });

    PP.Layout.mainRegion.show(Grid.productView);
  }

  // Event Handlers
  // -----------------------

  PP.vent.bind('grid:requested', function(categoryData){
    // use bootstrapped data on first start
    if (PP.App.bootstrappedCategoryName !== categoryData.categoryName) {
      Grid.productCollection.fetch({
        data: {
          gender: getGenderCode(categoryData.categoryName),
          category: categoryData.categoryId
        }
      });
    }
    else {
      PP.vent.trigger('mainview:ready', {
        categoryName: PP.App.bootstrappedCategoryName
      });
    }
  });

  // Initializer
  // --------------------

  PP.addInitializer(function(options){
    Grid.productCollection = new Grid.ProductCollection();

    Grid.productCollection.on('reset', function(){
      Grid.renderGrid(Grid.productCollection);
    });

    Grid.productCollection.reset(options.newArrivalsList);
  });
});
  • 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-09T04:35:36+00:00Added an answer on June 9, 2026 at 4:35 am

    the general guidelines is that any time you have an object that is created and destroyed throughout the life of the application, and that object needs to bind to events from some other object, you should use the EventBinder.

    Views And Memory Leaks

    Views are the perfect example of this. Views get created and destroyed all the time. They also bind to a lot of different events from both the model and collection on the view. When the view is destroyed, those events need to be cleaned up. By using the built-in bindTo method on the view, the event handlers will be cleaned up for you. If you don’t use bindTo and instead use on directly, you’ll have to manually call off to unbind the events when the view is closed / destroyed. If you don’t, you’ll end up with zombies (memory leaks).

    If you haven’t read them yet, check out these articles:

    http://lostechies.com/derickbailey/2011/09/15/zombies-run-managing-page-transitions-in-backbone-apps/

    http://lostechies.com/derickbailey/2012/03/19/backbone-js-and-javascript-garbage-collection/

    Custom Event Groupings

    Views are not the only place that this applies, though, and not the only use case for EventBinder.

    If you are working with handful of objects, binding to their events, and those objects can be replaced with other object instances, then an EventBinder would be useful. In this case, think of the EventBinder as a collection or group of events for related objects.

    Let’s say you have ObjA, ObjB and ObjC. Each of these fires some events, and you want to make sure you clean up the event handlers when your code is done. This is easy with an EventBinder:

    
    
    doStuff = {
    
      start: function(a, b, c){
        this.events = new Marionette.EventBinder();
        this.events.bindTo(a, "foo", this.doSomething, this);
        this.events.bindTo(b, "bar", this.anotherThing, this);
        this.events.bindTo(c, "baz", this.whatever, this);
      },
    
      doSomething: function(){ /* ... */ },
      anotherThing: function(){ /* ... */ },
      whatever: function(){ /* ... */ },
    
      stop: function(){
        this.events.unbindAll();
      }
    
    }
    
    doStuff.start(ObjA, ObjB, ObjC);
    
    // ... some time later in the code, stop it
    
    doStuff.stop();
    

    Calling stop in this code will properly clean up all the event handlers for this use.

    When To Use on/off Directly

    The converse to all of this, is to say that you don’t always need to use an EventBinder. You can get away without it, always. But you need to remember to clean up your events when necessary.

    In situations that do not need event handlers to be cleaned up, there is no need to use an EventBinder, as well. This may be an event that a router triggers, or that the Marionette.Application object triggers. For those application lifecycle events, or events that need to live throughout the entire life of the app, don’t bother with an EventBinder. Instead, just let the event handlers live on. When the person refreshes the browser window, or navigates to a different site, the handlers will be cleaned up at that point.

    But when you need to manage your memory and event handlers, cleaning up references and closing things down without refreshing the page or navigating away from it, then EventBinder becomes important as it simplifies event management.

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

Sidebar

Related Questions

this time someone should please relpy i am struggling with running my code using
I don't know what happened differently this time - my local env is running
This time I really don't know where to look for this problem. I have
Modified the code this time the objective to count the line of code was
this time, I'm facing a really weird problem. I've the following code: $xml =
I used FireFox throughout all this time and didn't see that. But when I
This time my test code displays a TextBox and two TextBlocks. The TextBox and
At this time, I have this piece of code for my employee class. But
All this time (specially in Netflix contest), I always come across this blog (or
I've got a reasonably simple query (this time) that I need ALL the results

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.