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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:26:23+00:00 2026-06-17T15:26:23+00:00

Backbone JS Views are very helpful in that they always creates a DOM node

  • 0

Backbone JS Views are very “helpful” in that they always creates a DOM node for you, with a configurable tag, id and class. This is very nice and accommodating, but I’m finding that it creates an unfortunate situation: the DOM node created by the View is a hidden template.

This became obvious to me on our current project, where we are sharing Mustache templates between the front and back ends. With Backbone, when you want a DOM that looks like this:

<section class="note">
  <textarea>
  ...
  </textarea>

  <input type="button" class="save-button" value="Save">
  <input type="button" class="cancel-button" value="Cancel">
</section>

you end up creating templates that look like this:

<textarea>
{{& content}}
</textarea>

<input type="button" class="save-button" value="Save">
<input type="button" class="cancel-button" value="Cancel">

But now your template is tied to the secret root-node template on your Backbone view, which will need to be duplicated on the server side. So much for DRY encapsulation!

I don’t see an immediately obvious way to address this, except by using setElement() with the rendered template at render time, but this brings other problems with it, like having to replace the newly rendered subtree in the DOM after every render().

How have you addressed this issue?

  • 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-17T15:26:25+00:00Added an answer on June 17, 2026 at 3:26 pm

    It’s an interesting question. I’ve never had to solve this particular problem before, but I tried out a few options and I think I found one that I like.

    First, here’s the code:

    //A base view which assumes the root element of its
    //template (string, DOM node or $) as its el.
    var RootlessView = Backbone.View.extend({
    
        //override the view constructor
        constructor: function(options) {
    
            //template can be provided by constructor argument or
            //as a subclass prototype property
            var template = options.template || this.template;
    
            //create a detached DOM node out of the template HTML
            var $el = Backbone.$(template).clone()
    
            //set the view's template to the inner html of the element
            this.template = $el.html(); 
    
            //set the element to the template root node and empty it
            this.el = $el.empty()[0];
    
            //call the superclass constructor
            Backbone.View.prototype.constructor.apply(this, arguments);
        }
    });
    

    Essentially you define a base view that expects every derived view to have a template property, or to take a template as an argument in the options hash. The template can be a string, a DOM node or a jQuery/Zepto -wrapped DOM node. The view assumes the root node of the template as its el, and redefines the template property as the contents of the root node.

    You would use it as a normal view:

    var View = RootlessView.extend({
        template: templateHtml,
        render: function() {
            this.$el.html(Mustache.render(this.template, this.model));
            return this;
        }
    }); 
    

    The el property is available from the get-go, and it’s not detached and reattached on re-render. The only exception to the normal Backbone.View behavior is that if you’ve defined the id, cssClass or tagName properties or arguments, they will be ignored, because the template provides the root element.

    This is not extensively tested, but seems to pass most simple test cases. The only drawback that I can think of is that the template html string is stored on every view instance (instead of the prototype) and wastes precious bytes of memory, but that shouldn’t be hard to solve either.

    Here is a working demo on JSFiddle.

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

Sidebar

Related Questions

I have the following class that extends Backbone.View, I want all my backbone views
My question is very close to Setting id and className dynamically in Backbone.js views
I've seen backbone views (or models, collections etc), declared like this var SomeView =
I'm having issues loading a collection of Backbone Views with RequireJS - as they
I have the view class FoursquareSearch.Views.Origin extends Backbone.View events: 'change [name=origin]': 'setOrigin' 'click [name=geolocate]'
I have been struggling with a form in one of my Backbone views. This
I'm trying to add a BaseView that performs some initialization for all Backbone views
I have a very complex Backbone application with many views/Models and collections. at times
Going through a very basic tutorial on backbone.js views. The expected behavior is to
If a Backbone View creates new Views inside its render() method, should these Views

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.