【问题标题】:Backbone collection view rendering主干集合视图渲染
【发布时间】:2013-09-18 20:39:31
【问题描述】:

我在渲染我的收藏时遇到问题......当我添加到控制台时,所有这些都在工作:

$('body').append(tablesView.render().el);

我从 json 文件中看到了我在 li 中的所有名字。之后我可以:

tablesCollection.create({ name:'Next Table Name' });

添加下一个立即渲染的对象。

我的代码:

 window.App = {

    Models: {},
    Views: {},
    Collections: {}
};

window.template = function (id) {

    return _.template($('id' + id).html());
};

//============================= 表模型

App.Models.Table = Backbone.Model.extend({

    defaults: {
            name: 'Table Name',
        },
});

//======================= 表格集合

App.Collections.Tables = Backbone.Collection.extend({

    model: App.Models.Table,

    url: 'tables.json'
});

//============== 从集合中查看每个表模型

App.Views.Tables = Backbone.View.extend({

    tagName: 'ul',


    initialize: function() {
        this.collection.fetch({reset:true});
        this.collection.on('reset', this.render);
        this.collection.on('add', this.addOne, this );

    },

    render: function () {

        this.collection.each(this.addOne, this);

        return this;

        },

    addOne: function(table) {

        var table = new App.Views.Table({ model: table });

        this.$el.append( table.render().el );

        table.render();

        }

});

//============================== 查看一个表模型

App.Views.Table = Backbone.View.extend({

    tagName: 'li',

    initialize: function() {

      this.model.on('destroy', this.remove, this)  

    },

    render: function () {

        this.$el.html( this.model.get('name') );

        return this;

    },
});

//================ 新集合实例

var tablesCollection = new App.Collections.Tables();

//========================= 新表查看实例

var tablesView = new App.Views.Tables({ collection: tablesCollection });

非常感谢您的每一个回答!!!! 问候 万花筒

【问题讨论】:

    标签: javascript backbone.js underscore.js


    【解决方案1】:

    试试这样的:

    var fetch = mycollection.fetch();
    fetch.done(function(){
       $(body).html(_.template(mytemplate, {obj: obj});
    });
    

    在初始化集合视图中添加:

    App.Views.Tables = Backbone.View.extend({
    
        el: '#content',
    
        template: _.template('<h1>My template</h2>'),
    
        initialize: function() {
            this.collection = new App.Collections.Tables();
            this.collection.fetch({reset:true});
            this.collection.on('reset', this.render);
            this.collection.on('add', this.addOne, this );
    
        },
        render: function(){
            this.$el.html(this.template());
        }
    

    ....

    【讨论】:

    • 感谢您的回答!!现在我再次编辑了代码...请检查
    • (更新了我的答案),尝试添加重置以在表格视图中进行初始化。更新了 jfiddle jsfiddle.net/hfYHm/3
    • 看起来 this.collection 没有在视图中实例化。更新了我的答案。
    • 还是什么都没有......我已经添加了 console.log('initialized');在用你的代码初始化函数并且不工作......
    • 现在我添加了 tablesView = new App.Views.Tables();并且在控制台中是 Uncaught TypeError: Cannot call method 'empty' of undefined
    【解决方案2】:

    确保在尝试呈现集合之前等待异步fetch 操作的结果:

    tablesCollection.fetch({
        success : function(collection, response, options){
         var tablesView = new App.Views.Tables({ collection: tablesCollection });
         $(document.body).append(tablesView.render().el);
        }
    });
    

    【讨论】:

    • 感谢您的回答,但我可以在哪里添加您的代码?我在 jsfiddle.net/makromat/hfYHm/1 上有这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多