我遇到了同样的问题并尝试了此处解释的答案,但我也在使用 require.js 并不断收到找不到 #my_view 模板的错误。如果有人能澄清 Marionette 默认在哪里查找模板,那就太好了。
相反,我通过使用 text.js 插件 for underscore.js 解决了这个问题。这样,您实际上可以 使用纯html 文件作为模板,无需 将其嵌套在脚本标签中。以下是我的做法。
define(['backbone', 'underscore', 'marionette', 'text!tmpl/my_view.html'], function(Backbone, _, Marionette, view_t){
var MyView = Backbone.Marionette.ItemView.extend({
template : function(serialized_model) {
//define your parameters here
param1 = erialized_model.param1;
return _.template(view_t)({
param1: param1
});
}
});
return MyView;
});
我将 text.js 插件与我的所有其他 js 库放在同一个 lib 目录中,并且我的 main.js for require 将模板的路径声明为
'tmpl': '../../templates',
我的项目结构是这样的
root
index.html
js
main.js
app
App.js
views
MyView.js
lib
require.js
text.js
backbone.js
underscore.js
jquery.js
backbone.marionette.js
templates
my_view.html
我的模板“my_view.html”看起来像这样。
<h1>THIS IS FROM THE TEMPLATE!!!</h1>
完美运行。我希望你觉得它有用。