Render JSON data to backbone.js template

10,384

If you don't wan't to modify your JSON file, you could change parse function in your PersonCollection to return you the person array. Example:

var PersonCollection = Backbone.Collection.extend({
    model: Person,
    url: '/people.json',
    parse: function (response) {
        // Return people object which is the array from response
        return response.people
    }
});

Backbone Documentation: http://backbonejs.org/#Model-parse

parse is called whenever a model's data is returned by the server, in fetch, and save. The function is passed the raw response object, and should return the attributes hash to be set on the model. The default implementation is a no-op, simply passing through the JSON response. Override this if you need to work with a preexisting API, or better namespace your responses.

Share:
10,384
xEmptyCanx
Author by

xEmptyCanx

Updated on June 30, 2022

Comments

  • xEmptyCanx
    xEmptyCanx almost 2 years

    I've looked around a ton, and can't find an answer to this issue. I'm trying to take a local JSON file, load it up using Backbone.js and render it to a template in the browser. My file downloads, and the template appears, but it is never populated by the data. Any thoughts? Thanks in advance.

    HTML

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>People list</title>
      <link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.1.1/css/bootstrap.min.css">
    </head>
    <body>
    
    
      <div class="container">
        <h1>People list</h1>
        <hr />
        <div class="page"></div>
      </div>
    
    
      <script type="text/template" id="people-template">
    
        <table class="table striped">
          <thead>
            <tr>
              <th>First Name</th>
              <th>Last Name</th>
              <th>Age</th>
              <th>Photo</th>
              <th>Video</th>
            </tr>
          </thead>
          <tbody>
            <% _.each(PersonCollection, function(Person) { %>
              <tr>
                <td><%= Person.get("firstName") %></td>
                <td><%= Person.get("lastName") %></td>
                <td><%= Person.get("age") %></td>
                <td><%= Person.get("photo") %></td>
                <td><%= Person.get("video") %></td>
              </tr>
            <% }); %>
    
          </tbody>
        </table>  
      </script>
    
      </body>
    
      <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js" type="text/javascript"></script>
      <script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js" type="text/javascript"></script>
      <script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js"></script>
    

    JAVASCRIPT

      <script>
    
        // MODEL MODEL MODEL
        // MODEL MODEL MODEL
    
        var Person = Backbone.Model;
    
        // COLLECTION COLLECTION COLLECTION
        // COLLECTION COLLECTION COLLECTION
    
        var PersonCollection = Backbone.Collection.extend({
          model: Person,
          url: '/people.json',
          parse: function (response) {
            return response
    }
        });
    
        // VIEWS VIEWS VIEWS
        // VIEWS VIEWS VIEWS
    
        var About = Backbone.View.extend ({
          el: '.page',
          render: function () {
            var that = this;
            var people = new PersonCollection();
            people.fetch({
              success: function (PersonCollection) {
                var template = _.template($('#people-template').html(), {PersonCollection: PersonCollection.models});
                that.$el.html(template);
              }
            })
          }
        });  
    
    
        var About = new About ();
    
        // ROUTES ROUTES ROUTES
        // ROUTES ROUTES ROUTES    
    
        var Router = Backbone.Router.extend({
          routes: {
            '': 'home'
          }
        });
    
        var router = new Router();
        router.on('route:home', function () {
          About.render();
        });
    
        Backbone.history.start();
    
      </script>
    

    JSON SAMPLE

    {
      "people": [
        {
          "firstName": "Jane",
          "lastName": "Doe",
          "age": "32",
          "photo": "test_photo",
          "video": "test_video"
        },
        {
          "firstName": "James",
          "lastName": "Hamm",
          "age": "56",
          "photo": "test_photo",
          "video": "test_video"
        },
    

    Thanks again for any suggestions. I'm new to stackoverflow (first question posted) so let me know if I need to provide more information.