[英]uncaught TypeError: Cannot call method 'replace' of undefined backbone.js
I 'm trying to develop a simple RSS app using backbone.js. 我正在尝试使用Beta.js开发一个简单的RSS应用程序。 I 'm using this backbone.js tutorial .
我正在使用这个ribs.js 教程 。 I 'm getting the following error, on line 2(template), when defining the template.
在定义模板时,在第2行(模板)上出现以下错误。 Can someone also tell me why is tagName: "li" defined in the tutorial?
谁能告诉我为什么在教程中定义了tagName:“ li”?
uncaught TypeError: Cannot call method 'replace' of undefined backbone.js
未捕获的TypeError:无法调用未定义的ribs.js的方法“替换”
Javscript Javscript
window.SourceListView = Backbone.View.extend({
tagName:"li",
template: _.template($('#tmpl_sourcelist').html()),
initialize:function () {
this.model.bind("change", this.render, this);
this.model.bind("destroy", this.close, this);
},
render:function (eventName) {
$(this.$el).html(this.template(this.model.toJSON()));
return this;
},
close:function () {
$(this.el).unbind();
$(this.el).remove();
}
});
HTML 的HTML
<script type="text/template" id="tmpl_sourcelist">
<div id="source">
<a href='#Source/<%=id%>'<%=name%></a>
</div>
</script>
thanks 谢谢
You're getting your error right here: 您在这里遇到了错误:
template: _.template($('#tmpl_sourcelist').html()),
Part of _.template
's internals involves calling String#replace
on the uncompiled template text on the way to producing the compiled template function. _.template
的内部结构的一部分涉及在生成编译的模板函数的方式上,对未编译的模板文本调用String#replace
。 That particular error usually means that you're effectively saying this: 该特定错误通常意味着您实际上是在说这句话:
_.template(undefined)
That can happen if there is no #tmpl_sourcelist
in the DOM when you say $('#tmpl_sourcelist').html()
. 当您说
$('#tmpl_sourcelist').html()
时,如果DOM中没有#tmpl_sourcelist
,则会发生这种情况。
There are a few simple solutions: 有一些简单的解决方案:
<script>
order so that your #tmpl_sourcelist
comes before you try to load your view. <script>
顺序,以使#tmpl_sourcelist
出现在您尝试加载视图之前。 Create the compiled template function in your view's initialize
instead of in the view's "class" definition: 在视图的
initialize
而不是在视图的“类”定义中创建已编译的模板函数:
window.SourceListView = Backbone.View.extend({ tagName:"li", initialize:function () { this.template = _.template($('#tmpl_sourcelist').html()); //...
As far as tagName
goes, the fine manual has this to say: 就
tagName
而言, 精美的手册说:
el
view.el
埃尔
view.el
[...]
this.el
is created from the view'stagName
,className
,id
andattributes
properties, if specified.[...]
this.el
是从视图的tagName
,className
,id
和attributes
属性(如果指定)创建的。 If not, el is an emptydiv
.如果不是,则el为空
div
。
So having this in your view: 因此,在您看来:
tagName: 'li'
means that Backbone will automatically create a new <li>
element as your view's el
. 表示Backbone将自动创建一个新的
<li>
元素作为视图的el
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.