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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:46:52+00:00 2026-06-05T08:46:52+00:00

I’m building my first web app using Backbone.js. So far so good. I’m looking

  • 0

I’m building my first web app using Backbone.js. So far so good. I’m looking for a little guidance:

I have a <ul> that I want to populate with <li> that represent ‘products’

This is what I’ve defined so far:

  • 1 model -> product
  • 1 collection -> products
  • 1 view -> view_product (view that represents a single product — one <li>)

I’ve created an instance of the collection by passing it an array of objects that represent my products, I understand this creates instances of the model for each product inside the collection.

I’m confused about what to do next.
Should I loop over the collection with _.each(), create and instance of view_product for each model in products and then try to append them to my <ul>? Something tells me that’s the wrong idea.

  • 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-05T08:46:54+00:00Added an answer on June 5, 2026 at 8:46 am

    I’ve created an instance of the collection by passing it an array of objects that represent my products, I understand this creates instances of the model for each product inside the collection.

    You understand correctly.

    You’ll probably want to add one more view for the collection of products, this view would be a <ul> and it would manage the per-product views (which are <li>s):

    var V = Backbone.View.extend({
        tagName: 'ul',
        initialize: function() {
            _.bindAll(this, 'render');
            this.collection.on('reset', this.render);
            // And other events as needed...
        },
        render: function() {
            var _this = this;
            this.collection.each(function(p) {
                var v = new view_product({ model: p });
                _this.$el.append(v.render().el);
            });
            return this;
        }
    });
    

    Collections already have many Underscore methods mixed in so you don’t need to _.each(collection.models, ...), collection.each(...) works just as well and is more idiomatic.

    Then you’d instantiate and render one of your per-collection views:

    var v = new V({ collection: your_existing_products_collection });
    $(some_container).append(v.render().el);
    

    Backbone treats collection options to views specially, that’s where this.collection comes from in initialize.

    The per-product views would take care of binding to the product’s events (such as name, price, quantity, … changes) and would take care of adding/removing products from the person’s cart. The per-collection view is responsible for anything involving the collection as a whole: collection resets, new products are added, products are removed, …

    Here’s a quick’n’dirty demo to help illustrate what I’m talking about: http://jsfiddle.net/ambiguous/SSstN/1/

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

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a French site that I want to parse, but am running into
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.