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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:22:02+00:00 2026-05-31T05:22:02+00:00

In my Backbone.js app, I need to be able to insert new items between

  • 0

In my Backbone.js app, I need to be able to insert new items between existing items in a collection. The examples I have found thus far on the web all seem to assume that new items should be appended to the end of a collection. That doesn’t work for my purposes, so I’ve opted to rerender the entire collection when a new item is added.

However, the original items aren’t removed from the view; instead, a duplicate set of items is appended to the end of the list. I’m able to work around this by using jQuery to clear the items prior to rendering, but that seems wrong.

Here’s what I have right now:

Item = Backbone.Model.extend({
    price: null,
});

Items = Backbone.Collection.extend({
    model: Item, 
    initialize: function () {
      this.add(new Item({ price: '$0.50' }));
      this.add(new Item({ price: '$0.60' }));
      this.add(new Item({ price: '$0.70' }));
    }
});

ItemView = Backbone.View.extend({
    tagName: 'li',
    initialize: function () {
      this.model.bind('change', this.render, this);
    },

    render: function () {
      var item_template = _.template($('#item-template').html(), { item: this.model }); 
      this.$el.html(item_template);
      return this;
    },

    events: {
      "click .copy-item": "copyItem",
    },

    copyItem: function (event) {
      var index = itemlistview.collection.indexOf(this.model);
      itemlistview.collection.add(new Item, { at: index + 1 });
    },
});

ItemListView = Backbone.View.extend({
    el: '#item-rows',
    initialize: function () {
      _.bindAll(this);
      this.collection = new Items();
      this.collection.bind('add', this.render);
      this.render();
    },
    render: function () {
      // It works if I uncomment the line below
      //$('.item-row').remove();
      var self = this;
      this.collection.each(function (item) {
        self.$el.append(new ItemView({ model: item }).render().el);
      });
      return this;
    },
});

var itemlistview = new ItemListView;

And here’s a jsFiddle that demonstrates the problem.

Is there a better way to handle this?

  • 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-31T05:22:03+00:00Added an answer on May 31, 2026 at 5:22 am

    As you’re rerendering the whole thing, you do need to clear the old rendered inputs.

    http://jsfiddle.net/Zk9NX/8/

    Altered ItemListView

    ItemListView = Backbone.View.extend({
        el: '#item-rows',
        initialize: function () {
          _.bindAll(this);
          this.collection = new Items();
          this.collection.bind('add', this.render);
          this.render();
        },
        render: function () {
          var self = this;
          this.$el.empty()
          this.collection.each(function (item) {
            self.$el.append(new ItemView({ model: item }).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 learning Backbone.js for a new app I'm building. I need to perform an
I have a web app and I need print the data into pdf. In
I'm using Backbone.js in a Rails app and I need to do file uploads
I have a question about the architecture of a backbone.js web app. Let's say
I have a backbone app with over 50 small JavaScript files. Now I want
I have a backbone.js app ( www.github.com/juggy/job-board ) where I want to bind my
I have the following backbone.js controller: App.Controllers.PlanMembers = Backbone.Controller.extend({ routes: { message/:messageType: sendMessage, :
I'm using backbone.js for my web app and it works semi-well so far. The
In my backbone.js app, there is a Trips collection that holds Trip models ,
I have a view in backbone.js App.Backbone.UserView = Backbone.View.extend({ tagName: 'li', className: 'pp-entry group',

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.