Javascript 未捕获的类型错误:无法调用未定义的backbone.js 的方法“替换”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14826149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
uncaught TypeError: Cannot call method 'replace' of undefined backbone.js
提问by jsp
I 'm trying to develop a simple RSS app using backbone.js. I 'm using this backbone.js tutorial. I 'm getting the following error, on line 2(template), when defining the template. Can someone also tell me why is tagName: "li" defined in the tutorial?
我正在尝试使用backbone.js 开发一个简单的RSS 应用程序。我正在使用这个backbone.js教程。定义模板时,我在第 2 行(模板)上收到以下错误。有人也可以告诉我为什么在教程中定义了 tagName: "li" 吗?
uncaught TypeError: Cannot call method 'replace' of undefined backbone.js
未捕获的类型错误:无法调用未定义的backbone.js 的方法“替换”
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
谢谢
回答by mu is too short
You're getting your error right here:
你在这里得到你的错误:
template: _.template($('#tmpl_sourcelist').html()),
Part of _.template's internals involves calling String#replaceon the uncompiled template text on the way to producing the compiled template function. That particular error usually means that you're effectively saying this:
的部分_.template内部结构涉及String#replace在生成编译模板函数的过程中调用未编译模板文本。该特定错误通常意味着您实际上是在说:
_.template(undefined)
That can happen if there is no #tmpl_sourcelistin the DOM when you say $('#tmpl_sourcelist').html().
如果#tmpl_sourcelistDOM 中没有$('#tmpl_sourcelist').html().
There are a few simple solutions:
有几个简单的解决方案:
- Adjust your
<script>order so that your#tmpl_sourcelistcomes before you try to load your view. Create the compiled template function in your view's
initializeinstead of in the view's "class" definition:window.SourceListView = Backbone.View.extend({ tagName:"li", initialize:function () { this.template = _.template($('#tmpl_sourcelist').html()); //...
- 调整您的
<script>订单,以便您#tmpl_sourcelist在尝试加载视图之前到达。 在视图的
initialize而不是视图的“类”定义中创建编译的模板函数:window.SourceListView = Backbone.View.extend({ tagName:"li", initialize:function () { this.template = _.template($('#tmpl_sourcelist').html()); //...
As far as tagNamegoes, the fine manualhas this to say:
就目前tagName而言,精美的手册是这样说的:
el
view.el[...]
this.elis created from the view'stagName,className,idandattributesproperties, if specified. If not, elis an emptydiv.
埃尔
view.el[...]
this.el是从视图的创建tagName,className,id和attributes属性,如果指定。如果不是,则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.

