简体   繁体   English

在backbone.js中交替使用2个不同的模板

[英]Alternating between 2 different templates in backbone.js

I have 2 different templates for my model's views. 我的模型视图有2个不同的模板。 Each time the models are fetched from the database, the first 3 models (#1, 2, 3) fetched from the backend will have the view created using the first template, the next 4 models (#4, 5, 6, 7) will use the second template, the next 3 models (#8, 9, 10) will use the first template and so on. 每次从数据库中提取模型时,从后端获取的前3个模型(#1,2,3)将使用第一个模板创建视图,接下来的4个模型(#4,5,6,7)将使用第二个模板,接下来的3个模型(#8,9,10)将使用第一个模板,依此类推。

Problem: How will I introduce this alternating template using backbone.js? 问题:如何使用backbone.js引入此交替模板?

JS Code JS代码

// Views

PhotoListView = Backbone.View.extend({
    el: '#photo_list',

    render: function() {
        $(this.el).html('');
        _.each(this.model.models, function(photo) {
            $(this.el).append(new PhotoListItemView({ model: photo }).render().el);
        }, this);
        return this;
    }
});

PhotoListItemView = Backbone.View.extend({
    tagNAme: 'div',
    className: 'photo_box',

    template: _.template( $('#tpl_PhotoListView').html() ),

    initialize: function() {
        this.model.bind('destroy', this.close, this);
    },

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

    close: function() {
        this.unbind();
        this.remove();
    }
});

First of all, your PhotoListView is wrapping a collection so you should be using this.collection inside the view and new PhotoListView({ collection: c }) to create it. 首先,您的PhotoListView正在包装一个集合,因此您应该在视图中使用this.collection并使用new PhotoListView({ collection: c })来创建它。 Views treat the collection option similarly to how they treat model : 视图处理collection选项与处理model方式类似

constructor / initialize new View([options]) 构造函数/初始化 new View([options])

[...] There are several special options that, if passed, will be attached directly to the view: model , collection , el , id , className , tagName and attributes. [...]有几个特殊选项,如果通过,将直接附加到视图: modelcollectionelidclassNametagName和attributes。

Using the right names will help prevent confusion. 使用正确的名称有助于防止混淆。 Also, views have some Underscore methods already mixed in so you can say this.collection.each(...) instead of _.each(this.collection.models, ...) or _(this.collection.models).each(...) . 此外,视图中已经混合了一些 this.collection.each(...) 方法 ,因此您可以说this.collection.each(...)而不是_.each(this.collection.models, ...)_(this.collection.models).each(...) You can also use this.$el instead of $(this.el) . 您也可以使用this.$el而不是$(this.el)

And now on to your real problem. 现在谈谈你的真正问题。 You can add two templates to your per-model view: 您可以为每个模型视图添加两个模板:

PhotoListItemView = Backbone.View.extend({
    template0: _.template($('#the-first-template-id').html()),
    template1: _.template($('#the-other-template-id').html()),
    //...
});

and an option to tell it which one to use: 并告诉它使用哪一个:

initialize: function(options) {
    this.template = this['template' + options.template_number];
    //...
}

Then you just need to specify the group option from the collection view. 然后,您只需要从集合视图中指定group选项。 Underscore's each passes the iteration index to the callback function as the second argument so you just need a bit of integer math to figure out which template_number to use: Underscore each将迭代索引作为第二个参数传递给回调函数,因此您只需要一些整数数学来确定要使用的template_number

this.collection.each(function(photo, i) {
    // Grouped in threes and you're alternating between two templates.
    var n = Math.floor(i / 3) % 2; 
    var v = new PhotoListItemView({ model: photo, template_number: n });
    this.$el.append(v.render().el);
}, this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM