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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:56:31+00:00 2026-06-04T01:56:31+00:00

I am trying to populate a collection with data from a JSON file. I’m

  • 0

I am trying to populate a collection with data from a JSON file. I’m a backbone beginner, so my problems are probably easy to solve. But have struggled with this all day – so now I’m asking for guidance.

I am creating a questionnaire, and want to load the questions from a JSON file stored locally (I will get the questions from a server later).

I am not sure if my collection gets populated at all, or if the problem is that the view doesn’t update. I have read a lot of tutorials and have got some ideas from different places.

Well.. this is my javascript code:

$(function(){

var Question = Backbone.Model.extend({

    defaults: {
        q: "Empty question..."
    },

    initialize: function() {
        if (!this.get("q")) {
            this.set({"q": this.defaults.q});
        }
    }
});


var QuestionList = Backbone.Collection.extend({
    model: Question,
    url: "question.json"
});

var Questions = new QuestionList;


var QuestionView = Backbone.View.extend({

    tagName:  "li",

    template: _.template($('#item-template').html()),

    initialize: function() {
        _.bindAll(this, 'render', 'remove');
        this.model.bind('change', this.render);
    },

    render: function() {
        $(this.el).html(this.template(this.model.toJSON()));
        return this;
    }
});


var AppView = Backbone.View.extend({

    el: $("#questionnaire_app"),

    itemTemplate: _.template($('#item-template').html()),

    initialize: function() {
        _.bindAll(this, 'addOne', 'addAll', 'render');

        Questions.bind('reset',   this.addAll);
        Questions.fetch();
    },

    addOne: function(question) {
        var view = new QuestionView({model: question});
        this.$("#question-list").append(view.render().el);
    },

    addAll: function() {
        Questions.each(this.addOne);
    }
});

var App = new AppView;

});

And I have the following HTML code:

<div id="questionnaire_app">
    <div class="title">
        <h1>Questions</h1>
    </div>
    <div class="content">
        <div id="questions">
            <ul id="question-list"></ul>
        </div>
    </div>
</div>

<script type="text/template" id="item-template">
    <div class="question">
        <div class="display">
            <p class="question-content"><%= q %></p>
        </div>
    </div>
</script>

The JSON file looks like this:

[
    {
        "q": "How were your meeting with Dr. Apfelstrudel?"
    },
    {
        "q": "What do you think of the movie Die Hard 4.0"
    },
    {
        "q": "Are people from Mars really green?"
    }
]

Bonus question: I use firebug, but find it very difficult to debug this stuff. If i e.g write console.log(Questions); in the console do I get ReferenceError: Questions is not defined. Why is that?

UPDATE: Problem solved. I didn’t use a web server before, which I do now (I just opened the file in the browser). The code works fine.

  • 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-04T01:56:33+00:00Added an answer on June 4, 2026 at 1:56 am

    I tested your code and it seems to run fine by me.

    EDIT: Here’s a link to a working fiddle with your code, just modified that instead of fetching the models I manually add the list (since I don’t see how i can duplicate that in the jsfiddle). Maybe your problem is with your JSON file (read only, wrong location).

    The reason why you get a reference error in firebug is because the questions reference going out of scope at the end of the document ready function, if you assign it to something you will be able to log it.

    For example

    var q = {};
    
    $(function() {
    
      // Your code
      var Questions = new QuestionList;
       q.Questions = Questions
     //the rest of your code
    });
    

    Now you can log it in firebug using console.log(q.Questions);

    • 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 UITableView with data from json result. I can
I'm trying to populate a drop down list with data from an SQL database
I'm trying to populate a List of beans from a form: public class Foo
I am trying to populate a variable with an XML response from an ajax
I am trying to populate a ListView in a separate class with data taken
I am trying to get data from a specific column in a database table
I am trying to populate some text boxes on a form with data pulled
I am trying to populate a listview from sqlite database. My code is using
I am using ASP.NET MVC 3. I am trying to populate my YUI data
Hey! I am trying to put some data from a textbox to an existing

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.