Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 不同类型的主干重复模板_Backbone.js - Fatal编程技术网

Backbone.js 不同类型的主干重复模板

Backbone.js 不同类型的主干重复模板,backbone.js,Backbone.js,我正试图找出使用主干视图和模板构造以下内容的最佳方法。我们有一个“消息”集合,但是消息可能有不同的类型,每个类型都有自己的视图。因此,基础集合可能如下所示: { { id: 1, name="message one", type="customer-message" }, { id: 2, name="message two", type="support-message" }, { id: 3, name="attachment one", type="attachment" }

我正试图找出使用主干视图和模板构造以下内容的最佳方法。我们有一个“消息”集合,但是消息可能有不同的类型,每个类型都有自己的视图。因此,基础集合可能如下所示:

{ 
  { id: 1, name="message one", type="customer-message" },
  { id: 2, name="message two", type="support-message" },
  { id: 3, name="attachment one", type="attachment" }
}
生成的页面输出类似于:

<ul>
  <li class="message customer-message"></li>
  <li class="message support-message"></li>
  <li class="message attachment"></li>
</ul>
这样,每个不同的li类将具有完全不同的结构/内容

我想弄清楚的是如何设置模板和视图a)处理嵌套,b)处理内部模板因类型不同而不同的事实

<script type="text/template" id="chat-template">
  <ul>
    <!--how to reference other templates here?-->
  </ul>
</script>

<script type="text/template" id="customer-message-template">
</script>
<script type="text/template" id="support-message-template">
</script>
<script type="text/template" id="attachment-template">
</script>

这里没有寻找完整的解决方案,只是希望有人能给我举个例子


谢谢

看看主干网。木偶网,它提供了视图“子类”,专门用于处理这样的收集场景

以下是我最后做的事情——基本上使用了约定优先于配置的方法(Robert Levy关于木偶网的帽子提示——不确定这是否也适用于直主干网,但我认为木偶网的CollectionView确实让这变得更容易):


将此标记为答案,因为它为我指明了正确的方向-请参阅我的“答案”,以了解有关实现的更多详细信息。
MyApp.MessageView = Backbone.Marionette.ItemView.extend({
    tagName: 'li',
    getTemplate: function() {
        var type = this.model.get("type");
        return "#" + type + "-template";
    }
});

MyApp.MessageCollectionView = Backbone.Marionette.CollectionView.extend({
    itemView: MyApp.MessageView,
    tagName: 'ul'
});