我正在关注本教程(仅限法语)https://github.com/k33g/articles/blob/master/2011-08-14-BB-VIEWS.md,它使用带有 Backbonejs 的下划线模板。
该教程说将此模板放在索引文件中。
<script type="text/template" id="doc-template">
<span><%= id %></span>
<span><%= title %></span>
<span><%= test %></span>
<span><%= keywords %></span>
</script>
<div id='doc-container'></div>
我将它放在 index.html.erb 中,但是,教程作者没有使用 rails。我有必要使用 erb,因为我还使用 rails content_for helpers 包含页面特定内容。
当我尝试查看页面时,我收到未定义的局部变量或方法错误
undefined local variable or method `id' for #<#<Class:0x007fd9c3a133b8>:0x007fd9c5066d90>
如果我从模板中删除这些变量,它仍然不会将内容呈现到页面。
谁能解释我在渲染数据方面做错了什么?
其他Backbone视图相关代码
本教程在适当的容器中初始化并呈现视图...
el : $('#doc-container'),
initialize : function() {
this.template = _.template($('#doc-template').html());
_.bindAll(this, 'render');
this.model.on('change', this.render);
},
render : function() {
var renderedContent = this.template(this.model.toJSON());
$(this.el).html(renderedContent);
return this;
}
更新
当我按照集合视图的教程说明进行操作时,我遇到了同样的问题。下划线的 each 方法会引发错误
undefined local variable or method `_' for #<#<Class:0x007fd9c3a133b8>:0x007fd9c2c78a78>
index.html.erb 中的模板
<script type="text/template" id="docs-collection-template">
<ol>
<% _.each(docs, function(doc) { %>
<li><%= doc.id %> : <%= doc.title %></li>
<% }); %>
</ol>
</script>