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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:12:35+00:00 2026-06-11T15:12:35+00:00

This is a 5 part backbone.js hello world tutorial/application. https://web.archive.org/web/20180317062059/http://arturadib.com/hello-backbonejs/docs/3.html In part 3, the

  • 0

This is a 5 part backbone.js hello world tutorial/application. https://web.archive.org/web/20180317062059/http://arturadib.com/hello-backbonejs/docs/3.html In part 3, the author’s illustrating how to use collections and models to store data and how to tie changes to the views.

I understand most of it, except this line

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

Is this ‘bind’ just binding context, or is it functioning as an ‘event’ such appendItem is called anytime that a model has been added?

I ask, because in the render method of the ListView, it’s explicitly calling appendItem method, so why is it bound to ‘add’

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

Can someone please explain a little how that code is working. i looked through the documentation but couldn’t find an explanation of bind used in this way.

Full code

(function($){
¶
Item class: The atomic part of our Model. A model is basically a Javascript object, i.e. key-value pairs, with some helper functions to handle event triggering, persistence, etc.

  var Item = Backbone.Model.extend({
    defaults: {
      part1: 'hello',
      part2: 'world'
    }
  });      

¶
List class: A collection of Items. Basically an array of Model objects with some helper functions.

  var List = Backbone.Collection.extend({
    model: Item
  });

  var ListView = Backbone.View.extend({
    el: $('body'),
    events: {
      'click button#add': 'addItem'
    },
¶
initialize() now instantiates a Collection, and binds its add event to own method appendItem. (Recall that Backbone doesn't offer a separate Controller for bindings...).

    initialize: function(){
      _.bindAll(this, 'render', 'addItem', 'appendItem'); // remember: every function that uses 'this' as the current object should be in here

      this.collection = new List();
      this.collection.bind('add', this.appendItem); // collection event binder

      this.counter = 0;
      this.render();      
    },
    render: function(){
¶
Save reference to this so it can be accessed from within the scope of the callback below

      var self = this;      
      $(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
        self.appendItem(item);
      }, this);
    },
¶
addItem() now deals solely with models/collections. View updates are delegated to the add event listener appendItem() below.

    addItem: function(){
      this.counter++;
      var item = new Item();
      item.set({
        part2: item.get('part2') + this.counter // modify item defaults
      });
      this.collection.add(item); // add item to collection; view is updated via event 'add'
    },
¶
appendItem() is triggered by the collection event add, and handles the visual update.

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

  var listView = new ListView();
})(jQuery);
  • 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-11T15:12:36+00:00Added an answer on June 11, 2026 at 3:12 pm

    Say your collection already has 10 models. Then you pass it to your view. You’d call render() which triggers a loop of appendItem() or what not. Your view is happy.

    Then you add a model to your collection. (Model 11)

    Rather than re-render the whole thing, the this.collection.on('add', this.appendItem, this) executes the function that adds a single item view to the already existing view list.

    That’s probably why it’s bound to the add event AND included in the render as a loop. One loops through an existing collection to generate views at the start. One takes care of any new models that are added after the view is initialized and rendered the first time.

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

Sidebar

Related Questions

I'm reading a Backbone tutorial http://coenraets.org/blog/2011/12/backbone-js-wine-cellar-tutorial-part-1-getting-started/ on building a wine cellar application. The author
I try to fetch this server: http://cshosting.webfactional.com/api/v1/projects/?format=json to a backbone.js collection. Then I try
I have this part of script from my GAE application which uses webapp2, which
I want to code this part of a VB6 application in c#. How can
I was looking to this example Introduction to Backbone.js Part 2 . In this
I am following this backbone rails turotial . The tutorial says they are using
I've got this piece of code from a Backbone.js tutorial from here . The
I have been experimenting with the localstorage module for Backbone.js (https://github.com/jeromegn/Backbone.localStorage). As I understand
I am testing backbone.js using Backbone.js Wine Cellar Tutorial — Part 1: Getting Started
I'm following a tutorial here: http://blog.chariotsolutions.com/2012/01/from-list-to-details-view-using.html I am trying to dynamically bind a click

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.