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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:33:04+00:00 2026-05-27T10:33:04+00:00

I am trying to populate a collection from a simple JSON file as part

  • 0

I am trying to populate a collection from a simple JSON file as part of learning backbone.js. But I can’t get it to work.

The AJAX call is made (verified with FireBug), but the toJSON method returns undefined.

What am I doing wrong?

theModel =  Backbone.Model.extend();

theCollection = Backbone.Collection.extend({
    model: aModel,
    url: "source.json"
});

theView = Backbone.View.extend({
   el: $("#temp"),
   initialize: function () {
       this.collection = new theCollection();
       this.collection.fetch();
       this.render();
   },
   render : function () {
       $(this.el).html( this.collection.toJSON() ); // Returns blank
   }
});

var myView = new theView;

Here’s my JSON:

[{
    "description": "Lorem ipsum..."
 },
 {
    "description": "Lorem ipsum..."
}]
  • 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-27T10:33:04+00:00Added an answer on May 27, 2026 at 10:33 am

    fetch is asynchronous, your collection won’t yet be populated if you immediately call render. To solve this problem, you just have to bind the collection reset event (sync event for Backbone>=1.0) to the view render :

    theView = Backbone.View.extend({
       el: $("#temp"),
    
       initialize: function () {
           this.collection = new theCollection();
    
           // for Backbone < 1.0
           this.collection.on("reset", this.render, this);
    
           // for Backbone >= 1.0
           this.collection.on("sync", this.render, this);
    
           this.collection.fetch();
       },
    
       render : function () {
        console.log( this.collection.toJSON() );
       }
    });
    

    Note the third argument of the bind method, giving the correct context to the method:
    http://documentcloud.github.com/backbone/#FAQ-this

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

Sidebar

Related Questions

I am trying to populate a collection with data from a JSON file. I'm
I trying to populate the dropdown list when page was loaded.But it is not
I am trying to populate a ListView ....but I cant add 2 string values
I am trying to populate an array with the text value of a json
I am trying to populate an array with a path to a file. <?php
I am trying to populate a spinner, but am getting an error. Here is
I am trying to populate the list li from the LoginAction on to the
I'm trying to get the backbone-relational plugin working with an association between tasks and
I'm trying to get a DataGridComboBoxColumn working with my ViewModel. Everything appears to work
We are trying to send a collection of objects from our page to our

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.