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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:52:57+00:00 2026-06-04T13:52:57+00:00

I am using Backbone.js for my project. Sorry, I am a Backbone newbie, therefore

  • 0

I am using Backbone.js for my project.
Sorry, I am a Backbone newbie, therefore I may be missing something very trivial.

This is an HTML snippet:

<table id="notes" class="table">
    <thead>
        <tr><th>title</th><th>comment</th></tr>
    </thead>
    <tbody id="notesTableBody">
    </tbody>
</table>

And this is the Backbone code that it is supposed to “inject” HTML in that snippet:

App.view.NoteListView = Backbone.View.extend({

    el:"tbody#notesTableBody",

    initialize:function () {
        this.model.bind("reset", this.render, this);
    },

    render:function (eventName) {
        _.each(this.model.models, function (note) {
            $(this.el).append(new App.view.NoteListItemView({model:note}).render().el);
        }, this);
        return this;
    }

});

For clarity, I haven’t pasted the NoteListItemView code because I don’t think it is relevant.

My problem is that the HTML code rendered by Backbone is as follows:

<table id="notes" class="table">
    <tbody id="notesTableBody">
    <tr><td>title 1</td><td>comment 1</td></tr>
    </tbody>
</table>

Basically Backbone removed the thead from the table.
I don’t understand why – how can I make sure Backbone doesn’t remove it?

  • 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-04T13:52:59+00:00Added an answer on June 4, 2026 at 1:52 pm

    Your NoteListView won’t write anything outside of its el (for example: http://jsfiddle.net/ambiguous/cFvX2/) so your problem is elsewhere.

    My guess was that you were doing something equivalent to this:

    var v = new App.view.NoteListView;
    $('#notes').html(view.render().el);
    

    and that will completely replace the content of the #notes table with just the <tbody> (i.e. NoteListView‘s el).

    There are couple ways around your problem. You could just call render and ignore what it returns:

    var v = new App.view.NoteListView;
    v.render();
    

    That’s pretty much what my first demo fiddle did.

    Or you could use id and tagName instead of el:

    App.view.NoteListView = Backbone.View.extend({
        id: 'notesTableBody',
        tagName: 'tbody',
        initialize:function () {
            this.model.bind("reset", this.render, this);
        },
        render:function (eventName) {
            _.each(this.model.models, function (note) {
                $(this.el).append(new App.view.NoteListItemView({model:note}).render().el);
            }, this);
            return this;
        }
    });
    

    and then append the view’s el to $('#notes'):

    var v = new App.view.NoteListView;
    $('#notes').append(v.render().el);
    

    Demo: http://jsfiddle.net/ambiguous/HTAkM/

    You could also let the caller specify the el:

    App.view.NoteListView = Backbone.View.extend({
        initialize:function () {
            this.model.bind("reset", this.render, this);
        },
        render: function(eventName) {
            _.each(this.model.models, function (note) {
                $(this.el).append(new App.view.NoteListItemView({model:note}).render().el);
            }, this);
            return this;
        }
    });
    

    and then render into el without caring about render‘s return value:

    var v = new App.view.NoteListView({ el: $('#notesTableBody') });
    v.render();
    

    Demo: http://jsfiddle.net/ambiguous/2Ptkp/


    And while I’m here, you don’t need to $(this.el) anymore, recent versions of Backbone provide this.$el for your views:

    $el view.$el

    A cached jQuery (or Zepto) object for the view’s element. A handy reference instead of re-wrapping the DOM element all the time.

    If your view is wrapping a collection, you should use this.collection instead of this.model:

    new SomeView({ collection: some_collection })
    

    Views treat the collection option specially just like model:

    constructor / initialize new View([options])

    […] There are several special options that, if passed, will be attached directly to the view: model, collection, el, id, className, tagName and attributes.

    Backbone collections also have several Underscore methods mixed in so you don’t need to say this:

    _.each(some_collection.models, function(m) { ... });
    

    you can call each right on the collection:

    some_collection.each(function(m) { ... });
    

    Also, if you’re binding to a "reset" event:

    this.model.bind("reset", this.render, this);
    

    you probably want your render to clear out the el before appending more things to it:

    render: function() {
        this.$el.empty();
        this.collection.each(function(note) { ... });
    }
    

    If you don’t empty the el you’ll be adding more things to it when you probably mean to replace its contents with new things.

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

Sidebar

Related Questions

This may be a general javascript or jQuery question- I'm using backbone.js and I'd
First sorry for my horrible english. This is the first time I'm using backbone.js.
I'm using Backbone and therefore Underscore to render my templates. My templates get rendered
I'm using backbone with the backbone-rails gem which does its own templating and project
Trying to get my head around backbone.js. This example is using Backbone Boilerplate and
Using the following as an example: var Case = Backbone.Model.extend({ initialize: function(){ this.bind(error, function(model,
I'm using backbone.js for a rails project I've got on the side. I'm embedding
I am working on an open source javascript project using backbone that uses a
I have been using Backbone on a new project and so far have loved
I am very new at using Backbone. Please forgive me in advance as I

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.