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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:06:50+00:00 2026-06-17T22:06:50+00:00

I am using backbone and i would like to parse my first collection in

  • 0

I am using backbone and i would like to parse my first collection in my view.

My first question, is undescore really the best way to do it? I have heard about mustache.js

The next thing is, i don’t know how to do is:

var A = new Model();
var B = new Model();
var Mole = new Collection([A, B]);
var View = View({model: A, collection: Mole });
View.render();

Here is my render method:

render: function(){
  this.template = _.template($('template').html());
  $(this.el).html(this.template(this.collection.models)); //I don't know what to send here
}

Here is my template

<script type="text/template" id="template">
  <% _.each(collection, function(model){ %> //I don't know what to get here
    <% model.name %>
  <% }); %>
</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-17T22:06:51+00:00Added an answer on June 17, 2026 at 10:06 pm

    First of all, _.template wants the text from the template, not a jQuery object. That means that this:

    this.template = _.template($('template'));
    

    should be this:

    this.template = _.template($('template').html());
    

    Then the compiled template function will want to see key/value pairs for the data; from the fine manual (this applies to Mustache, Handlebars, and Underscore BTW):

    When you evaluate a template function, pass in a data object that has properties corresponding to the template’s free variables.

    So you want to say this:

    this.$el.html(this.template({
        collection: this.collection.toJSON()
    }));
    

    and then you can say this in your template:

    <% _.each(collection, function(model) { %>
      <%= model.name %>
    <% }); %>
    

    A couple points to consider:

    1. Backbone views already have a jQuery wrapped this.el in this.$el so there’s no need to $(this.el).
    2. Serialized data is generally passed to templates using toJSON, this applies doubly so to Mustache and Handlebars since they won’t understand anything else.
    3. You need to say <%= ... %> to get some output in your template; <% ... %> simply evaluates a bit of JavaScript code, it won’t leave anything behind, you have to use interpolation delimiters (<%= and %> by default) for that.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using backbone.js here is my scenario Lets say I have a view
I have a tree of categories and I would like to do rending using
I'm using Backbone and Require.js. Everything works great but, I would like to add
Using Backbone.model, I would like to change the get method in order to get
Using jQuery and backbone.js, I would like to separate the JavaScript from the HTML
I'm using Rivets.js for two two-way data binding in a Backbone project and would
Currently I am using backbone-rails in my Rails app. I would like to know
I am using Backbone and I have a view with events defined: .... events:
I would like to know what is the recommaned way of nesting Backbone Views.
Using Backbone.js, I have a collection that I instanctiate, X . After instantiating it,

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.