Backbone.js无法呈现 [英] Backbone.js not rendering

查看:65
本文介绍了Backbone.js无法呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的收藏出于某种原因未呈现.无法找到原因.

My collection is not rendering for some reason. Cannot find out why.

TreeItem = Backbone.Model.extend({

});

TreeList = Backbone.Collection.extend({
  model: TreeItem,
  url: "/get_tree_list"
});

window.tree_list = new TreeList();

// VIEW

window.TreeItemView = Backbone.View.extend({
  tagName: 'li',
  initialize: function(){
    _.bindAll(this, 'render');
  },
  render: function(){
    $(this.el).html('<span>'+this.model.get('title')+'</span>');
    return this;
  }
});

window.TreeListView = Backbone.View.extend({
  el: "#tree-structure",
  events: {

  },
  initialize: function() {
    _.bindAll(this, 'appendItem', 'render');
    tree_list.bind('add', this.appendItem);
    tree_list.fetch();
    this.render();
  },
  render: function() {
    tree_list.each(this.appendItem);
    return this;
  },
  appendItem: function(item){
    var tree_item_view = new TreeItemView({
      model: item
    });
    $(this.el).append(tree_item_view.render().el);

  }
});

var tree_list_view = new TreeListView;

推荐答案

Backbone.js提供了很多需要解释的地方,这是新人们出错的地方.您的错误本质上是根本的.您将View直接绑定到模型上

Backbone.js provides a lot to be interpreted that's where people new go wrong. Your mistake is fundamental in nature. You tie the View directly to the model

  • 请参阅初始化函数,在该函数中呈现集合的实例!

  • see initialize function where a instance of collection is rendered!!

无论何时何地,只要创建模型,集合就作为参数传递给视图的构造函数.检查我的小提琴

Always and anywhere you create model, collection pass then as parameters to the constructor of views. Check my fiddle

永远不要在模型,视图或集合中调用渲染.它们必须在应用程序文件中

Never call render inside model, view or collection. They must be inside application file

http://jsfiddle.net/35QGM/

这篇关于Backbone.js无法呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆