backbone.js无法在ul内部渲染li

时间:2015-12-21 12:00:31

标签: backbone.js

对于backbone.js来说是新手,并且对视图渲染部分有疑问。请找下面的代码,我无法在ul.Html部分内打印li,如下所示

echo json_encode($markersData);

和js部分

response = JSON.parse(markersData, function(k,v){
                    return v;
                });
    console.log(response);

1 个答案:

答案 0 :(得分:1)

一个问题是您没有调用子视图的render方法。

更改

$(this.el).append( new listUsers({model:mode}).render.el

$(this.el).append( new listUsers({model:mode}).render().el

您的代码可以更好地编写如下:

var Book = Backbone.Model.extend();
var Books = Backbone.Collection.extend({
  model: Book,
  url: "http://localhost/bbq/books.php"
});
var ListUsers = Backbone.View.extend({
  template: _.template($('#item').html()),
  initialize: function() {
    this.render();
  },
  render: function() {
    this.$el.html(this.template(this.model.toJSON()));
    return this;
  }
});

var Booksview = Backbone.View.extend({
  el: '#listview',
  initialize: function() {
    _.bindAll(this, "render");
    this.collection.fetch({
      success: this.render
    });
  },
  render: function() {
    this.collection.each(function(model) {
      this.$el.append(new ListUsers({
        model: model
      }).el);
    }, this);
    return this;
  }
});

var books = new Books();
var view = new Booksview({
  collection: books
});