简体   繁体   English

骨干视图作为无序或有序的html列表的一部分

[英]Backbone view as part of an unordered or ordered html list

I have a problem - I have a list: 我有一个问题 - 我有一个清单:

<ol>
    <li>List item 1</li>
    <li>List item 2</li>
</ol>

This part is static and will never change. 这部分是静态的,永远不会改变。

Another button adds a Backbone view to this list without grouping it. 另一个按钮将Backbone视图添加到此列表而不对其进行分组。

As you know; 如你所知; you need an el tag on a view or at the very least a root tag as I may need to do further event capture on this view. 您需要在视图上或至少在根标记上使用el标记,因为我可能需要在此视图上进行进一步的事件捕获。 I cannot use a div tag as the resulting html would be invalid, this: 我不能使用div标签,因为生成的html无效,这:

<ol>
    <li>List item 1</li>
    <li>List item 2</li>
    <div class="myBackboneView">
        <li>List item for first component</li>
        <li>List item for second component</li>
    </div>
</ol>

(Cannot have div tags amongst those li) (这些li中不能有div标签)

Does anyone know of how I might be able to sneak a root tag without messing the html validation up. 有没有人知道如何在不弄乱html验证的情况下偷偷摸摸根标签。

Either that or is there a backbone trick that allows you to have a view linked together without using an el. 无论是那个还是有一个主干技巧,允许你在不使用el的情况下将视图链接在一起。

I'd also want to be able to add any number of these views - so I'd have a root view rooted to the ol tag and then within that view it would delegate to individual views for each of the li groups and so the problem is that I can't put a tag to group those li tags. 我还希望能够添加任意数量的这些视图 - 所以我有一个根视图到ol标记的根视图,然后在该视图中它将委托给每个li组的各个视图,因此问题是我不能把标签分组这些li标签。

I know you might think it should be a separate list but this is a list of things to do. 我知道您可能认为它应该是一个单独的列表,但这是一个要做的事情列表。 If they add this new view they have more things to do in the list - thus it makes sense for it to be part of one list. 如果他们添加这个新视图,他们在列表中有更多的事情要做 - 因此将它作为一个列表的一部分是有意义的。

Thanks for any help - I'm off to make meatballs. 谢谢你的帮助 - 我要去做肉丸了。

Try this: 尝试这个:

<ol>
    <li>List item 1</li>
    <li>List item 2</li>
    <li class="myBackboneView">
      <ol>
        <li>List item for first component</li>
        <li>List item for second component</li>
      <ol>
    </li>
</ol>

You could also put class="myBackboneView" on the inner ol . 你也可以把class="myBackboneView"放在内部ol

You can call setElement to set your view's el to a specific element that already exists: 您可以调用setElement将视图的el设置为已存在的特定元素:

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引用,并将视图的委托事件从旧元素移动到新元素。

So you can simply do this somewhere convenient (such as initialize or render ): 所以你可以在方便的地方(比如initializerender )这样做:

this.setElement($('ol'));

You might need a more specific selector of course. 当然,您可能需要更具体的选择器。

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

You could also set el manually in your view's definition: 您也可以在视图的定义中手动设置el

el: 'ol' // Or a more specific selector

Demo: http://jsfiddle.net/ambiguous/qk5Tg/1/ 演示: http//jsfiddle.net/ambiguous/qk5Tg/1/

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

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