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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:29:16+00:00 2026-06-14T12:29:16+00:00

I modified my Backbone app to render a large table using a DOM document

  • 0

I modified my Backbone app to render a large table using a DOM document fragment, which results in a nice performance improvement when there are many rows.

Unfortunately, this seems to break event handling, so that the views for each row no longer receive click events. I created a minimal example to demonstrate this.

What do I have to do to fix this?

// Javascript
$(window).load(function(){

  var Item = Backbone.Model.extend();

  var Items = Backbone.Collection.extend({
      model: Item,
      count: 5,
      initialize: function() {
        for(i=0; i<this.count; i++){
          this.add(new Item({text: i}));
        }
      }
  });

  var ItemView = Backbone.View.extend({
      tagName: 'tr',

      initialize: function(item) {
        this.item = item;
      },

      render: function() {
        this.$el.append('<td class="item">' + this.item.get('text') + '</td>');
        return this;
      },

      events: function() {
        return { "click .item": function() {
            console.log('handling click on item '+this.item.get('text'));
          }
        };
      }

  });

  var TableBodyView = Backbone.View.extend({
      tagName: 'tbody',

      render: function() {
          if(this.options.useFragment){
              this.renderWithFragment();
          } else {
               this.renderWithoutFragment();
          }
        return this;
      },

      renderWithFragment: function() {
          var fragment = document.createDocumentFragment();
          this.collection.each(function(item){
            fragment.appendChild((new ItemView(item)).render().el);
          });
          this.$el.append(fragment.cloneNode(true));
      },

      renderWithoutFragment: function() {
          self = this;
          this.collection.each(function(item){
              self.$el.append((new ItemView(item)).render().el);
          });
      }
  });

var items = new Items();
$('#t1').append((new TableBodyView({collection: items, useFragment: false})).render().el);
$('#t2').append((new TableBodyView({collection: items, useFragment: true})).render().el);
});

// HTML
<table id="t1" />
<table id="t2" />

// CSS
table { float: left; margin: 10px; padding: 5px; }
table#t1 { background: green; }
table#t2 { background: red; }
  • 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-14T12:29:17+00:00Added an answer on June 14, 2026 at 12:29 pm

    Your problem is that cloneNode explicitly doesn’t copy event handlers:

    Cloning a node copies all of its attributes and their values but does not copy event listeners.

    So, when you do this:

    this.$el.append(fragment.cloneNode(true));
    // No more events -------^ 
    

    you lose Backbone’s delegate attached to the view’s el and all your event handling is gone. If you want to clone the el and keep the events then you can use the withDataAndEvents flag with jQuery’s clone:

    .clone( [withDataAndEvents] [, deepWithDataAndEvents] )

    withDataAndEvents A Boolean indicating whether event handlers and data should be copied along with the elements. The default value is false.

    deepWithDataAndEvents A Boolean indicating whether event handlers and data for all children of the cloned element should be copied. By default its value matches the first argument’s value (which defaults to false).

    So you should be able to do it like this instead:

    this.$el.append($(fragment).clone(true));
    

    Demo (open your console please): http://jsfiddle.net/ambiguous/M6SvH/

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

Sidebar

Related Questions

!Modified! I have a table (mySQL 5.0.x) which contains values for date ranges. |
I modified the code published on smbrown.wordpress.com which can extract the top tracks using
I have an app modified to take into account the UAC in VISTA. So,
I modified a program which creates a Queue and then add or remove items
I modified the list.html example here https://github.com/tbranyen/backbone.layoutmanager/blob/master/examples/list.html a bit: http://jsfiddle.net/qhoc/nQJz6/ If you look into
Modified the below circular queue code for my app. This queue can hold 32
I have modified this jQuery pluggin to make a table that updates with an
I am using if-modified-since in the HTTP header to decide if I should download
I want to create a table that is sortable using the attributes of a
Is it a bad practice to have backbone views which do not depend on

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.