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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:23:53+00:00 2026-05-30T06:23:53+00:00

I just started learning Backbone.js, and have been working on (what else) a simple

  • 0

I just started learning Backbone.js, and have been working on (what else) a simple to-do application. In this app, I want to display my to-do items inside of <ul id="unfinished-taks"></ul> with each task as a <li> element. So far, so simple.

According to the tutorials I have read, I should create a View with the following:

// todo.js
window.TodoView = Backbone.View.extend({
    tagName: 'li',
    className: 'task',
    // etc...
});

This works fine, but it seems like bad practice to define the HTML markup structure of my to-do item inside of my Javascript code. I’d much rather define the markup entirely in a template:

// todo.js
window.TodoView = Backbone.View.extend({
    template: _.template($("#template-task").html()),
    // etc...
});

<!-- todo.html -->
<script type="text/template" id="template-task">
    <li class="task <%= done ? 'done' : 'notdone' %>"><%= text %></li>
</script>

However, if I do it that way Backbone.js defaults to using tagName: 'div' and wraps all my to-do items in useless <div> tags. Is there a way to have the HTMl markup entirely contained within my template without adding unsemantic <div> tags around every view element?

  • 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-30T06:23:55+00:00Added an answer on May 30, 2026 at 6:23 am

    If you are only planning to render the view once, you can set the el property of the view manually in .initialize():

    // todo.js
    window.TodoView = Backbone.View.extend({
        template: _.template($("#template-task").html()),
    
        initialize: function() {
            this.el = $(this.template(this.model.toJSON())).get(0);
        },
    
        // etc
    });
    

    There are some caveats here, though:

    • Backbone expects the el property to be a single element. I’m not sure what will happen if your template has multiple elements at the root, but it probably won’t be what you expect.

    • Re-rendering is difficult here, because re-rendering the template gives you a whole new DOM element, and you can’t use $(this.el).html() to update the existing element. So you have to somehow stick the new element into the spot of the old element, which isn’t easy, and probably involves logic you don’t want in .render().

    These aren’t necessarily show-stoppers if your .render() function doesn’t need to use the template again (e.g. maybe you change the class and the text manually, with jQuery), or if you don’t need to re-render. But it’s going to be a pain if you’re expecting to use Backbone’s standard “re-render the template” approach for updating the view when the model changes.

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

Sidebar

Related Questions

Just started learning PySide and is having problem with QTimer I have this #!/usr/bin/python
I just started learning RoR. This is my /app/trip_controller.rb file class TripController < ApplicationController
I just started learning C++ couple of weeks ago. So now I have this
Just started learning Haskell. I have an empty source file with this inside: pe
I just started learning some ruby, and I want to do something like this:
I just started learning C but I don't understand this part of the code.
I have just started learning Erlang and am trying out some Project Euler problems
I just started learning targets, and have followed all the instructions to create my
I just started learning Python... I am writing a simple program that will take
I just started learning about servers, and I am messing around with Google's App

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.