2

我一直在研究 Brian Mann 的 BackboneRails 截屏视频,所以我的应用程序结构与那些完全一致。

下面定义了 HomepageApp Marionette Appication 的“显示”部分。

My.module('HomepageApp.Show', function(Show, App, Backbone, Marionette, $, _){

  Show.Photo = Marionette.ItemView.extend({
    tagName:'span'
  });

  Show.Photos = Marionette.CollectionView.extend({  
    template: 'homepage/show/templates/photos',

    itemView:Show.Photo,
    itemViewContainer: '#photos'
  });

  Show.Layout = Marionette.Layout.extend({
    template: 'homepage/show/templates/layout',
    regions:{
      photoRegion: '#photo-region'
    }
  });
});

我还使用以下内容覆盖了 Marionette.Renderer.render 函数:

Backbone.Marionette.Renderer.render = function(template, data){
  var path = JST["backbone/apps/" + template];  

  try{
    if(!path) throw({message: "Template '" + template + "' not found!"}); 
    return path(data);
  }

  catch(err){
    console.log(err.message);
  }
}

我的所有观点,包括许多未在此处显示的观点都运行良好。问题是 Show.Photos CollectionView 的“模板”属性在我的渲染器覆盖中显示为“未定义”,这给了我以下错误:

Template 'undefined' not found!

奇怪的是,当我根本没有为模板属性传递任何值时,甚至会发生这种情况。

我也知道我在实例化时传递了一个有效的 Backbone 集合。

我完全被困住了。有谁熟悉这种现象?

4

1 回答 1

11

CollectionView 不应该有模板,为此您应该使用 CompositeView,如文档中所述:

CompositeView 扩展自 CollectionView 以用作复合视图,用于表示树结构中的分支和叶子的场景,或者用于需要在包装器模板中呈现集合的场景

你可以在这里阅读更多内容:https ://github.com/marionettejs/backbone.marionette/blob/master/docs/marionette.compositeview.md

于 2013-03-14T08:47:10.117 回答