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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:28:38+00:00 2026-05-24T07:28:38+00:00

I have a Backbone.js Collection and I have an array of model IDs that

  • 0

I have a Backbone.js Collection and I have an array of model IDs that I want to populate it. I know that I could fetch these objects one by one, build an array of objects and pass them into the Collection’s constructor as an array.

What I’d like to be able to do is pass the array of object ids into the constructor as initial data, and get the Collection to fetch them, possibly as batch, as per this.

Doable?

  • 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-24T07:28:39+00:00Added an answer on May 24, 2026 at 7:28 am

    When you call ‘fetch’ on a Backbone.Collection, it in turn calls Backbone.sync, which by default just asks the collection for the url to use.

    So if your server responds to:

    /models/batch/?ids=1,2,3,4
    

    You could do something like:

    var MyCollection = Backbone.Collection.extend({
    
        model: Model,
    
        url: '/models',
    
        initialize: function(models, options) {
            ids = options.ids || [];
            if (ids.length > 0) { 
                this.fetchByIds(ids);
            }
        },
    
        fetchByIds: function(ids) {
            // Save a reference to the existing url
            var baseUrl = this.url;
    
            // Assign our batch url to the 'url' property and call the server
            this.url += '/?ids=' + ids.join(',');
            this.fetch();
    
            // Restore the 'url' property
            this.url = baseUrl;
        }
    });
    

    And use it like so:

    var coll = new MyCollection({}, {ids: [1, 2, 3, 4]});
    

    You’d have to pass the ids in the options parameter, because the Backbone.Collection constructor function sets the models passed in the first parameter before it calls the ‘initialize’ function.

    Theoretically, this should work (read: completely untried).

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

Sidebar

Related Questions

I have a super simple Backbone model/collection that wraps around a facebook feed. window.Story
I have Backbone.js collection that holds (for example) 30 items. I want to pass
I have a backbone model which contains an array which I am updating with
I have the following Backbone.js model and collection: // Model lr.Event = Backbone.Model.extend({}); //
I have very simple backbone model and collection. I have a corresponding backbone.marionette.CollectionView and
My Backbone Collection receives 30 models on fetch(). I have tried newColl=origColl.first(2); to return
I have a backbone collection and when I remove a model from the collection,
I've got a Backbone.Collection full of models; let's say that model is Car .
let's say I have : var Book = Backbone.Model.extend(); var Collection = Backbone.Collection.extend({ model:
I have a web app that I want to use Backbone.js for. I have

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.