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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:49:40+00:00 2026-06-10T18:49:40+00:00

I am quite new to backbone and need use it to create a list

  • 0

I am quite new to backbone and need use it to create a list item. Each list item has a model, and a view. Because its a list it seems like an ideal solution for collections, but I’m struggling to use them.

Here is the current version, which I would like to chaneg to use collections:

// The Model & view
var IntroModel = Backbone.Model.extend({});
var Introview = Backbone.View.extend({
    template: _.template( $('#taglist-intro').text() ),
    render: function() {
        console.log( this.model.attributes );
        this.$el.append( this.template( this.model.attributes ) );
    }
});

// We will store views in here
// Ideally this would be a collection
views = [];

// Get the data for that collection
$.getJSON( url, function( json ) {

    _.each( json, function( item ) {

        // Create each model & view, store views in the views array
        var model = new IntroModel( item );
        var view = new Introview({
        model : model
        })  
        views.push( view );

    })

})

// I can render a view like this
// But I'd rather it rendered the view when I add items to the collection
views[0].render()

So what i have works, but its not really doing it ‘the backbone way’. Which seem a little pointless because:

  1. It would be better to use a collection, not an array
  2. It would be better that views render when items are added to the array
  3. Its not Backbone really is it..

Grateful for any pointers, if you cant provide specific code examples I’d still be very grateful to links & resources covering this issue.

Cheers,

Richard

  • 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-10T18:49:41+00:00Added an answer on June 10, 2026 at 6:49 pm

    Your right that the current implementation is not the Backbone way. Most of what you are doing is handled directly by the collection object in backbone. In backbone collections are essentially just an array with additional methods attached to them. These methods are what gives collections their power. Backbone has a number of features including:

    • ‘url’ property: using this property the collection will automatically populate itself when you run the fetch method (e.g. myCollection.fetch() ).
    • You can bind a function to the ‘reset’ event of the collection. This event triggers when when you populate the collection. By including a call to your collection’s render event your collection can automatically render the related view when the collection changes. There are also other collection events (e.g. ‘add’ new model, etc) which you can also attach functions to.
    • I find the Backbone documentation to be the best place to start. However a simple example is always useful. The following code shows how a simple collection can be defined, and how you would create two views (one view which creates a list, and another view which renders the item within the list). Note the use of the url property in the collection. Backbone uses this to retrieve the content of the collection when you run the fetch() method (See OrgListView object). Also note how the view’s render method is bound to the collections ‘reset’ event, this ensures that the render event is called after populating the collection (See OrgsListView’s initialize method).

              /**
           * Model
           */
          var Org = Backbone.Model.extend();
      
          /**
           * Collection
           */
          var Orgs = Backbone.Collection.extend({
              model: Org,
              url: '/orgs.json'
          });
      
          /**
           * View - Single Item in List
           */
          var OrgItemView = Backbone.View.extend({
              tagName: 'li',
              initialize: function() {
                  _.bindAll(this, 'onClick', 'render');
                  this.model = this.options.model;
                  // Create base URI component for links on this page. (e.g. '/#orgs/ORG_NAME')
                  this.baseUri = this.options.pageRootUri + '/' + encodeURIComponent(this.model.get('name'));
                  // Create array for tracking subviews.
                  /*var subViews = new Array();*/
              },
              events: {
                  'click a.test': 'onClick'
              },
              onClick: function(event) {
                  // Prevent default event from firing.
                  event.preventDefault();
                  if (typeof this.listContactsView === 'undefined') {
                      // Create collection of contacts.
                      var contacts = new ContactsByOrg({ url: '/orgs.json/' + encodeURIComponent(this.model.get('name')) });
                      this.listContactsView = new ListContactsView({ collection: contacts, baseUri: this.baseUri });
                      this.$el.append(this.listContactsView.render().el);
                  }
                  else {
                      // Close View.
                      this.listContactsView.close();
                      // Destroy property this.listContactsView.
                      delete this.listContactsView;
                  }
              },
              onClose: function() {
      //      console.log('Closing OrgItemView');
              },
              render: function() {
                  // TODO: set proper value for href. Currently using a dummy placeholder
                  this.$el.html('<a class="test" href="' + this.baseUri + '">' + this.model.get('name') + '</a>');
                  return this;
              }
          });
      
          /**
           * View - List of organizations
           */
          var OrgsListView = Backbone.View.extend({
              className: 'orgs-list',
              initialize: function() {
                  console.log('OrgsListView');
                  _.bindAll(this, 'render');
                  this.pageRootUri = this.options.pageRootUri;
                  this.collection = this.options.collection;
                  // Bind render function to collection reset event.
                                      this.collection.on('reset', this.render);
                  // Populate collection with values from server.
                  this.collection.fetch();
              },
              onClose: function() {
                  this.collection.off('reset', this.render);
      //      console.log('Closing OrgsListView');
              },
              render: function() {
                  var self = this;
                  this.$el.html('<ul></ul>');
                  this.collection.each(function(org, index) {
                      var orgItemView = new OrgItemView({ model: org, pageRootUri: self.pageRootUri });
                      self.$('ul').append(orgItemView.render().el);
                  });
                  return this;
              }
          });
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm quite new in the world of Backbone and I decided to use Marionette
Iam quite new to functions in SQL and I would like to create a
Im quite new at Javascript however I have been trying to create a currency
I'm starting a new Backbone app, and I've been quite fond of the javascript
Im quite new to doctrine2. I need to make a connection like this: Tag
Im quite new in Scala. I need to get the parametrization of a class.
Quite new to Solr 1.4 - seems to be very powerful indeed. However, I
I am quite new to github. I worked on a project which has multiple
Quite new to coding for android but this issue has me tearing my hair
Maybe I'm misunderstanding something (quite possible as I'm new to Backbone), but I can't

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.