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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:18:13+00:00 2026-06-05T13:18:13+00:00

I am building a gallery-like picker view in Backbone.js. The picker has many thumbnail

  • 0

I am building a gallery-like picker view in Backbone.js. The picker has many thumbnail views bound to models within a collection and a large preview view for the currently selected thumbnail. Clicking a thumbnail will fire a Backbone.Event to change the preview view’s model. The preview view, however, observes several Backbone events on the model to change state depending on the model’s attributes.

I’m having trouble when it comes to unregistering Backbone events on the previous model and re-registering the same events on the new model. I don’t always have reference to the original .on() registration, and I am tempted to simply call this.model.off() to unregister all the model’s events (I don’t want to destroy any other events the model may have, however). The Backbone.js documentation outlines that calling .off(null, null, context) will unregister the events from the object within the current context. I am uncertain, however, if this will unregister all events just for the current view instance.

  • 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-05T13:18:17+00:00Added an answer on June 5, 2026 at 1:18 pm

    Let’s use this setup to register and unregister events:

    var ThumbView=Backbone.View.extend({
        initialize: function() {
            this.model.on("change:title", this.log, this);
        },
    
        log:function(model) {
            console.log("Thumb view : "+model.get("id")+" : "+model.get("title"));
        }
    });
    
    var MainView=Backbone.View.extend({
        initialize: function() {
            this.model.on("change:title", this.log, this);
        },
    
        log:function(model) {
            console.log("Main view : "+model.get("id")+" : "+model.get("title"));
        }
    });
    
    var m1=new Backbone.Model({id:1,title:"m1"});   
    var t=new ThumbView({model:m1});
    var v=new MainView({model:m1});
    
    m1.set({title:"m1, 1"});
    v=new MainView({model:m1});
    m1.set({title:"m1, 2"});
    

    As is, creating a new MainView won’t destroy the previous bindings and will result in a zombie view. The last 3 lines give the following result:

    Thumb view : 1 : m1, 1
    Main view : 1 : m1, 1
    Thumb view : 1 : m1, 2
    Main view : 1 : m1, 2
    Main view : 1 : m1, 2

    The accompanying Fiddle : http://jsfiddle.net/9xufW/

    Let’s test the off method with a specific context:

    var MainView=Backbone.View.extend({
        initialize: function() {
            this.model.on("change:title", this.log, this);
        },
    
        log:function(model) {
            console.log("Main view : "+model.get("id")+" : "+model.get("title"));
        },
    
        teardown: function() {
            this.model.off(null, null, this);
        }
    });
    

    Calling

    m1.set({title:"m1, 1"});
    v.teardown(); // "destroys" the old view
    
    v=new MainView({model:m1});
    m1.set({title:"m1, 2"});
    

    yields the expected result

    Thumb view : 1 : m1, 1
    Main view : 1 : m1, 1
    Thumb view : 1 : m1, 2
    Main view : 1 : m1, 2

    http://jsfiddle.net/9xufW/1/

    The callbacks set in the thumbviews are preserved while the callbacks set in the main view are removed. Note that the teardown method could and probably should be used to undelegate DOM events.

    Another Fiddle where the model in the main view is replaced instead of destroying/recreating the view http://jsfiddle.net/9xufW/2/

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

Sidebar

Related Questions

I finished building a photo gallery and would like to give users the ability
I'm building a photo gallery for a client, and I'd like them to be
I'm building a django app that has an image gallery, and the client insists
I'm building an image gallery and have been able to get the clicked thumbnail
I am building my own image gallery, Like fancybox . See: http://snabbdesign.com/stack/stack.html I am
Thanks for reading! I am building a custom Gallery app where the first thumbnail
Background: I'm building this photo gallery in AS3, when user clicks a thumbnail is
I am currently building a horizontal gallery of videos. I'd like to make something
I'm currently struggling with different resolutions when building my gallery-application . I've realized the
I'm building a rails gallery app for a company that doesn't want to host

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.