最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Backbone.js - Get JSON array into view template - Stack Overflow

programmeradmin1浏览0评论
window.User = Backbone.Model.extend({
  defaults: {
     name: 'Jane',
     friends: []
  },        

  urlRoot: "users",

  initialize: function(){
    this.fetch();
  }
});

  var HomeView = Backbone.View.extend({
    el: '#container',
    template: _.template($("#home-template").html()),

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

      home: function() {
        var user = new User({id: 1});
        this.homeView = new HomeView({
          model: user
        });
        this.homeView.render();
      },

The model data is being queried and the root level attributes work fine, but the attribute that contains an array of other objects doesn't seem to show up.

Template:

   <script id="home-template" type="text/template">
      <div id="id">
        <div class="name"><%= name %></div>
        <br />
        <h3> Single Friends</h3>
        <br />
        <ul data-role="listview" data-inset="true", data-filter="true">
          <% _.each(friends, function(friend) { %>
            <li>
              <a href="/profile?id=<%= friend.id %>", data-ajax="false">
                <div class="picture"><img src="/<%= friend.fb_user_id %>/picture"></div>
                <div class="name"><%= friend.name %></div>
              </a>
            </li>
          <% }); %>

        </ul>
      </div>
    </script>

Return JSON:

{"name":"John Smith","friends":[{"id":"1234","name":"Joe Thompson","fb_user_id":"4564"},{"id":"1235","name":"Jane Doe","fb_user_id":"4564"}]}

It almost seems like it's not seeing the .friends attribute at all because it's taking the defaults of the model ([]).

Any suggestions?

window.User = Backbone.Model.extend({
  defaults: {
     name: 'Jane',
     friends: []
  },        

  urlRoot: "users",

  initialize: function(){
    this.fetch();
  }
});

  var HomeView = Backbone.View.extend({
    el: '#container',
    template: _.template($("#home-template").html()),

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

      home: function() {
        var user = new User({id: 1});
        this.homeView = new HomeView({
          model: user
        });
        this.homeView.render();
      },

The model data is being queried and the root level attributes work fine, but the attribute that contains an array of other objects doesn't seem to show up.

Template:

   <script id="home-template" type="text/template">
      <div id="id">
        <div class="name"><%= name %></div>
        <br />
        <h3> Single Friends</h3>
        <br />
        <ul data-role="listview" data-inset="true", data-filter="true">
          <% _.each(friends, function(friend) { %>
            <li>
              <a href="/profile?id=<%= friend.id %>", data-ajax="false">
                <div class="picture"><img src="http://graph.facebook./<%= friend.fb_user_id %>/picture"></div>
                <div class="name"><%= friend.name %></div>
              </a>
            </li>
          <% }); %>

        </ul>
      </div>
    </script>

Return JSON:

{"name":"John Smith","friends":[{"id":"1234","name":"Joe Thompson","fb_user_id":"4564"},{"id":"1235","name":"Jane Doe","fb_user_id":"4564"}]}

It almost seems like it's not seeing the .friends attribute at all because it's taking the defaults of the model ([]).

Any suggestions?

Share Improve this question edited Apr 16, 2013 at 11:46 user2181408 asked Nov 23, 2011 at 14:31 user577808user577808 2,5274 gold badges22 silver badges31 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

You are calling render() before fetch() has returned the data from the server.

Try this?

window.User = Backbone.Model.extend({
  defaults: {
     name: 'Jane',
     friends: []
  },
  urlRoot: "users"
});

var HomeView = Backbone.View.extend({
  el: '#container',
  template: _.template($("#home-template").html()),

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

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

评论列表(0)

  1. 暂无评论