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

  • Home
  • SEARCH
  • 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 6769653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:13:08+00:00 2026-05-26T15:13:08+00:00

I have implemented a simple close() method for all the Backbone views which disposes

  • 0

I have implemented a simple close() method for all the Backbone views which disposes of a view when its not needed/needs to be reset.

Backbone.View.prototype.close = function() {
    if (this.onClose) {
        this.onClose();
    }
    this.remove();
    this.unbind();
};

NewView = Backbone.View.extend({
    el: '#List ul',
    initialize: function() {},
    render: function() {
       _(this.collection.models).each(function(item) {
            this.renderChildren(item);
       }, this);
    },
    renderChildren: function(item) {
        var itemView = new NewChildView({ model: item });
        $(this.el).prepend(itemView.render());
    },
    onClose: function() {
        this.collection.reset();
        // I want to remove the child views as well
    }
});

NewChildView = Backbone.View.extend({
    tagName: 'li',
    render: function() {
    }
});

Now, when I remove the parent view, I also want to remove all the child views here. Any ideas how can I can do this without looping through the models like this….

   _(this.collection.models).each(function(item) {
        item.close();
   }, this);
  • 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-05-26T15:13:09+00:00Added an answer on May 26, 2026 at 3:13 pm

    I think in most of the cases you should keep the view removal in the view layer, without affecting your models.

    For example, if you remove a view with comments, maybe another view in your app shows a selection of comments, or some statistics, and resetting the collection would affect those views too.

    So I think you should keep it all in the view (only relevant methods included):

    NewView = Backbone.View.extend({
        initialize: function() {
           this.childViews = [];
        },
        renderChildren: function(item) {
            var itemView = new NewChildView({ model: item });
            $(this.el).prepend(itemView.render());
            this.childViews.push(itemView);
        },
        onClose: function() {
          _(this.childViews).each(function(view) {
            view.close();
          });
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple form on a view page, implemented as a user control,
I have implemented a simple file upload-download mechanism. When a user clicks a file
I have implemented a simple linux shell in c. Now, I am adding some
In our desktop application, we have implemented a simple search engine using an inverted
Very simple Qt GUI application: On the scene I have multiple circles implemented as
I have the simple class using auto-implemented properies: Public Class foo { public foo()
I have a partial view (UserControl) that implements a simple pager in my Asp.Net
I have my business-logic implemented in simple static classes with static methods. Each of
This question is close to what I'm interested in, but not quite. I have
I have a method which returns an File object. I need to know when

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.