简体   繁体   English

Backbone.js我们可以没有任何tagName吗?

[英]Backbone.js Can we not have any tagName?

I just started with backbone.js, and one thing that I noticed is that sometimes I do not want any tagName to contain/encapsulate my view's template code. 我刚开始使用backbone.js,我注意到的一件事是,有时我不希望任何tagName包含/封装我的视图的模板代码。 If I leave it at '' or 'span' , I get unneccessary div and span in my code. 如果我把它留在'''span' ,我会在代码中得到不必要的divspan

The alternative that I found is to remove the containing tag from my template ( <div class="photo_box"> in my example as shown below), and use that as the tagName in my view. 我找到的替代方法是从我的模板中删除包含标记(在我的示例中为<div class="photo_box"> ,如下所示),并在我的视图中将其用作tagName Most of the time, this tag will contain a class ( .photo_box ), and I still have to perform an addClass to (this.el). 大多数情况下,此标记将包含一个类( .photo_box ),我仍然需要执行addClass (this.el)。 I dont really like scattering up my template code. 我真的不喜欢散布我的模板代码。

Is there another way? 还有另外一种方法吗?

JS JS

// Views
PhotoListView = Backbone.View.extend({
    tagName: 'span',

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

PhotoListItemView = Backbone.View.extend({
    tagName: 'span',

    template: _.template($('#tpl-PhotoListItemView').html()),

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


});

HTML HTML

<!-- Templates -->
<script type="text/template" id="tpl-PhotoListItemView">
                <div class="photo_box">
                    <div class="photo_container">
                        <img src="img/profiling/<%= photo_id %>.jpg" class='photo' />
                    </div>
                </div>
</script>

Result 结果

<div id="photo_list">
    <span>
        <span>
                    <div class="photo_box">
                        <div class="photo_container">
                            <img src="img/profiling/f_001.jpg" class="photo">
                        </div>
                    </div>
        </span>
        <span>
                    <div class="photo_box">
                        <div class="photo_container">
                            <img src="img/profiling/f_002.jpg" class="photo">
                        </div>
                    </div>
        </span>
    </span>
</div>

You could always use setElement : 你总是可以使用setElement

setElement view.setElement(element) setElement view.setElement(element)

If you'd like to apply a Backbone view to a different DOM element, use setElement , which will also create the cached $el reference and move the view's delegated events from the old element to the new one. 如果您想将Backbone视图应用于其他DOM元素,请使用setElement ,它还将创建缓存的$el引用,并将视图的委托事件从旧元素移动到新元素。

and forget about tagName completely: 并完全忘记了tagName

PhotoListItemView = Backbone.View.extend({
    template: _.template($('#tpl-PhotoListItemView').html()),
    render: function() {
        this.setElement(this.template(this.model.toJSON()));
        return this;
    }
});

Demo: http://jsfiddle.net/ambiguous/XWEMg/ 演示: http//jsfiddle.net/ambiguous/XWEMg/


As an aside, <span> is a poor choice for a container (even a temporary one) due to its limited permitted content ; 另外,由于容器内容有限, <span>对于容器(即使是临时容器)来说也是一个糟糕的选择; you risk the browser rearranging your HTML to get something valid if you start throwing arbitrary HTML into a <span> . 如果您开始将任意HTML放入<span>则可能会使浏览器重新排列HTML以获得有效的内容。 A <div> is a much safer choice as it can hold pretty much anything. <div>是一个更安全的选择,因为它几乎可以容纳任何东西。

You don't need to manually add the class-name. 您无需手动添加类名。 You can use the className property: 您可以使用className属性:

PhotoListItemView = Backbone.View.extend({
    tagName: 'span',
    className: 'photo_box',

Btw, I recommend this HTML structure: 顺便说一下,我推荐这个HTML结构:

<ul id="photo_list">
    <li>
        <img src="img/profiling/f_001.jpg" class="photo">
    </li>
    <li>
        <img src="img/profiling/f_003.jpg" class="photo">
    </li>
</ul>

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

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