Backbone.Marionette - 传递变量复合视图标签 [英] Backbone.Marionette - passing variable to composite view tag

查看:385
本文介绍了Backbone.Marionette - 传递变量复合视图标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

UPDATE(相关的细节):这种复合视图是合成视图集合中

我如何使用Backbone.Marionette复合视图构建以下HTML?

 < OPTGROUP标签=1组>
        <选项>项目1 LT; /选项>
        <选项>项目2'; /选项>
        <选项>项目3'; /选项>
    < / OPTGROUP>
    < OPTGROUP标签=2组>
        <选项>项目4℃; /选项>
        <选项>项目5℃/选项>
        <选项>项目6≤/选项>
    < / OPTGROUP>

因为我想避免的< D​​IV> 包装,我必须指定< OPTGROUP> 作为标记名。

 查看= Backbone.Marionette.CompositeView.extend({
    收藏:some_collection,
    ItemView控件:some_itemview,
    模板:!#some_template,< - 善有善报在模板中? - >
    itemViewContainer:OPTGROUP',
    标签名:OPTGROUP,< - 指定要避免的默认分区 - >    <! - 我应该为了通过标签=组1的标签OPTGROUP指定 - >
});


解决方案

不要使用CompositeView中的这一点。你并不需要一个包装的模板,因为在这种情况下,包装仅仅是< OPTGROUP方式> 标签

使用的CollectionView相反,它不会呈现一个包装的模板。

有关组#,使用的OnRender方法

 
鉴于= Backbone.Marionette.CollectionView.extend({
    收藏:some_collection,
    ItemView控件:some_itemview,
    标签名:OPTGROUP',    OnRender方法:功能(){
      这$ el.attr(标签,组1);
    }});

UPDATE (RELEVANT DETAIL): This composite view is within a collection of composite views.

How can I construct the following HTML using a Backbone.Marionette composite view?

    <optgroup label="Group 1">
        <option>Item 1</option>
        <option>Item 2</option>
        <option>Item 3</option>
    </optgroup>
    <optgroup label="Group 2">
        <option>Item 4</option>
        <option>Item 5</option>
        <option>Item 6</option>
    </optgroup>

Since I want to avoid the <div> wrapper, I will have to specify <optgroup> as the tagName.

view = Backbone.Marionette.CompositeView.extend({
    collection: some_collection,
    itemView: some_itemview,
    template: '#some_template', <!-- What goes inside the template? -->
    itemViewContainer: 'optgroup',
    tagName:'optgroup', <!--specify to avoid the default div-->

    <!-- What should I specify in order to pass label="Group1" to the optgroup tag-->
});

解决方案

Don't use a CompositeView for this. You don't need a wrapper template since the wrapper in this case is only the <optgroup> tag.

Use a CollectionView instead, which doesn't render a wrapper template.

For the group #, use the onRender method


view = Backbone.Marionette.CollectionView.extend({
    collection: some_collection,
    itemView: some_itemview,
    tagName:'optgroup',

    onRender: function(){
      this.$el.attr("label", "Group 1");
    }

});

这篇关于Backbone.Marionette - 传递变量复合视图标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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