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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:22:56+00:00 2026-05-25T13:22:56+00:00

I am following a tutorial I found on BackboneJs located here . About halfway

  • 0

I am following a tutorial I found on BackboneJs located here.

About halfway down in the render method he does the following:

    events: { 'click button#add': 'addItem' },

    initialize: function () { 
        this.collection = new List();

        // Collection event binder
        this.collection.bind('add', this.appendItem); 

        this.counter = 0;
        this.render();
    },

    render: function () {
        this.el.append("<button id='add'> Add List Item</button>");
        this.el.append("<ul></ul>");

        _(this.collection.models).each(function(item){ 
          // in case collection is not empty
            appendItem(item);
        }, this);
    },

    addItem: function () { 
        var item = new Item();

        this.counter++;

        item.set({
            part2: item.get('part2') + " " + this.counter 
        });

        this.collection.add(item);
    },

    appendItem: function (item) {
        $('ul').append('<li>' + item.get('part1') + " " + item.get('part2') + '</li>');
    }

I have a couple of questions about the line below.

_(this.collection.models).each(function(item){ 
   // in case collection is not empty
   appendItem(item);
}, this);

What does the underscore _ do in this context?

Why is this even needed?

The comment says in case the collection is not empty. However without that line it works just fine. And the bind override in the initialize function tells Backbone to run this.appendItem when the add event is triggered on the collection, or so I thought and confirmed by removing the line in question.

  • 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-25T13:22:56+00:00Added an answer on May 25, 2026 at 1:22 pm

    i think this method typically wraps an array in an “underscore” helper class, to give it access to all of the underscore.js helper methods. in this case, the .each method is coming from the underscore helper class.

    like you said, though, this should work fine without it. it may be that the version of backbone on which this tutorial was written (v0.3.3) requires the _ method for the array of models to be iterated like this.

    the documentation for underscore.js ( http://documentcloud.github.com/underscore/ ) talks about using _() as a method call, instead of using the library in an object-oriented mannter.

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

Sidebar

Related Questions

I'm following the tutorial located at: http://www.asp.net/learn/mvc/tutorial-20-cs.aspx I created my own .Menu() method to
I was trying to expand upon a tutorial I found here about creating a
I am following a tutorial on http://msdn.microsoft.com/en-us/wp7trainingcourse_usingbingmapslab_topic3#_Toc271039355 . Source code can be found here:
I'm following the Gtk Hello World tutorial found here , and I've come across
I'm following (or rather not following!) the tutorial found here: http://www.flashuser.net/flash-tricks/tips-tricks-10-using-drag-drop-in-actionscript-3-0.html I've drawn a
Hey guys I'm following the rails tutorial found here http://net.tutsplus.com/tutorials/ruby/the-intro-to-rails-screencast-i-wish-i-had/ and I've gotten to
I found the following table structures while I was watching ruby on rails tutorial.
I am following the following tutorial ( http://www.highoncoding.com/Articles/642_Creating_a_Stock_Widget_in_ASP_NET_MVC_Application.aspx ) on using ajax to render
I'm following the tutorial here http://applicake.com/posts/54-integrating-facebook-connect-with-rails-applications to set up facebook connect. It almost worked,
I am following the tutorial http://www.bit-101.com/blog/?p=2115 . In this tutorial I found a project

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.