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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:49:08+00:00 2026-06-12T18:49:08+00:00

I was trying out Backbonejs read through couple of docs. It is an MVC

  • 0

I was trying out Backbonejs read through couple of docs. It is an MVC
framework for client side. Inside the views we compile a template and
render them(attach them to DOM, or do some manipulation). Backbone has
a dependency of underscore js, which is the templating stuff.

With Bbone, when manipulating views el comes into picture. My
understanding of el is that it references the DOM Object. If no
dom object is assigned to it, it assumes an empty div.

var choose_view = new SearchView({ el: $(“#choose_me”) });

So in the above case, the div with id choose_me will be manipulated,
when the choose_view is invoked. So far so good, but what are the
stuff happening below in chronology, I was unable to get, also is
there any redundancy, read the comments for the queries:

// the div with id search_container will be picked up by el
<div id="search_container"></div>

<script type="text/javascript">
    SearchView = Backbone.View.extend({
        initialize: function(){
            this.render();
        },
        render: function(){
            //1. Compile the template using underscore, QUESTION: **what does this mean**?
            var template = _.template( $("#search_template").html(), {} );
            //2. Load the compiled HTML into the Backbone "el"
            this.el.html( template );
        }
    });
    // QUESTION: **When in 2. we have already mentioned, el, can we not over there
    provide the div element?**
    var search_view = new SearchView({ el: $("#search_container") });
</script>

<script type="text/template" id="search_template">
    <label>Search</label>
    <input type="text" id="search_input" />
    <input type="button" id="search_button" value="Search" />
</script>
  • 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-12T18:49:09+00:00Added an answer on June 12, 2026 at 6:49 pm

    Question 1

    Compiling template means that you get a template function that you can pass in a data object as argument of the template function. That way, you can evaluate many times your template function with different data objects while it’s compiled only once.

    compiledTemplate = _.template( $("#search_template").html());
    this.el.html(compiledTemplate(data));
    another.el.html(compiledTemplate(otherData));
    

    In the example above you compile your template once, then you use it twice.
    In the code you have given, you compile and use your template at the same time

    So

    _.template( $("#search_template").html()); // Create a template function, the result is a function that you can call with a data object as argument
    _.template( $("#search_template").html(), data); // Create a template function and call it directly with the data object provided, the result is a string containing html
    

    Ref: http://underscorejs.org/#template

    Question 2

    You can provide the div element directly, but el is an helper to retrieve the root element of your view on which all DOM events will be delegated.

    As indicated in the backbone doc

    If you’d like to create a view that references an element already in
    the DOM, pass in the element as an option: new View({el:
    existingElement})

    Ref: http://backbonejs.org/#View-el

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

Sidebar

Related Questions

I am new to backbone and trying out my first app after going through
I´m trying out entity framework included in VS2010 but ´ve hit a problem with
With the below javascript code i am trying some BackboneJs concepts. Couldn't figure out
I'm trying out lisp and working through the book Practical Common Lisp at http://www.gigamonkeys.com/book/
I'm trying out backbonejs but got stuck on how to bind the model to
I am trying to build a switching view with backbone js and found out
I am trying out Backbone.Marionette and I am confused as to why my Layouts
Trying out the example usage of codebrew rails-backbone , everything works fine but the
I'm starting out with backbone and I'm trying to create a simple view that
I am very new to Backbone and just trying to figure out the way

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.