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

  • Home
  • SEARCH
  • 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 8206403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T08:39:09+00:00 2026-06-07T08:39:09+00:00

Just getting started with Backbone. I have a generic view that can render a

  • 0

Just getting started with Backbone. I have a generic view that can render a collection as a list with a title. I’m currently passing the collection and title into the render method, but that seems a bit odd. Is there another way that’s more canonical?

E.g.:

var ListView = Backbone.View.extend({
    template: _.template([
        "<div>",
        "<% if (title) { %><h2><%= title %></h2> <% } %>",
        "<% if (items.length > 0) { %>",
        "<ul>",
            "<% items.each(function(item) { %>",
            "<%= itemTemplate(item) %>",
            "<% }); %>",
        "</ul>",
        "<% } else { %><p>None.</p><% } %>",
        "</div>"
    ].join('')),

    itemTemplate: _.template(
        "<li><%= attributes.name %> (<%= id %>)</li>"
    ),

    render: function(items, title) {
        var html = this.template({
            items: items /* a collection */,
            title : title || '',
            itemTemplate: this.itemTemplate
        });

        $(this.el).append(html);
    }
});

var myView = new ListView({ el: $('#target') });
myView.render(myThings, 'My Things');
myView.render(otherThings, 'Other Things');
  • 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-07T08:39:12+00:00Added an answer on June 7, 2026 at 8:39 am

    You should pass attributes in the initialize() function:

    initialize: function (attrs) {
        this.options = attrs;
    }
    

    So here you would pass the attributes as an object, like so:

    new MyView({
      some: "something",
      that: "something else"
    })
    

    Now you have the values that you passed in accessible throughout this instance, in this.options

    console.log(this.options.some) # "something"
    console.log(this.options.that) # "something else"
    

    To pass in a collection, I recommend making one parent view and one subview:

    var View;
    var Subview;
    
    View = Backbone.View.extend({
        initialize: function() {
            try {
                if (!(this.collection instanceof Backbone.Collection)) {
                    throw new typeError("this.collection not instanceof Backbone.Collection")
                }
                this.subViews = [];
                this.collection.forEach(function (model) {
                    this.subViews.push(new SubView({model: model}));
                });
            } catch (e) {
                console.error(e)
            }
        },
        render: function() {
            this.subViews.forEach(function (view) {
                this.$el.append(view.render().$el);
            }, this);
            return this;
        }
    });
    
    SubView = Backbone.View.extend({
        initialize: function () {
            try {
                if (!(this.model instanceof Backbone.model)) {
                    throw new typeError("this.collection not instanceof Backbone.Collection")
                }
            } catch (e) {
                console.error(e);
            }
        },
        render: function () {
            return this;
        }
    });
    
    testCollection = new MyCollection();
    collectionView = new View({collection: testCollection});
    $("body").html(collectionView.render().$el);
    

    You should always handle the Models of a Collection, not just the data of the collection.

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

Sidebar

Related Questions

Just getting started with Obj-C and iOS programming. I have some code that loads
Just getting started with backbone.js, and one of the things I've noticed is that
I am just getting started couchdb and have been looking into writing couch apps.
I'm just getting started with a project and wondered if someone can let me
Hi I'm just getting started on the v4 CTP so I can see me
Just getting started on Scala Actors. The Scala website says: Thread-blocking operations can be
I'm just getting started with knockout.js. I have a foreach binding like this, which
I'm just getting started using Mustache and I have this rendering problem. I send
Just getting started with Backbone.js. Simply including Backbone (either dev/production versions) causes the error:
I am just getting started with Backbone.js , but it looks really interesting... Right

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.