未捕获类型错误:不能调用方法'取代'Backbone.js的未定义的 [英] uncaught TypeError: Cannot call method 'replace' of undefined backbone.js
问题描述
我试图开发使用Backbone.js的一个简单的RSS应用程序。我使用这个Backbone.js的教程 。我在收到以下错误,在2号线(模板),定义模板时。
可有人还告诉我,为什么是标签名:礼的教程中定义
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的
uncaught TypeError: Cannot call method 'replace' of undefined 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
<script type="text/template" id="tmpl_sourcelist">
<div id="source">
<a href='#Source/<%=id%>'<%=name%></a>
</div>
</script>
感谢
推荐答案
你让你的错误就在这里:
You're getting your error right here:
template: _.template($('#tmpl_sourcelist').html()),
_。模板的一部分
的内部需要调用<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace\"><$c$c>String#replace$c$c>上的方式来生产编译模板函数未编译模板文本。特定错误通常意味着你有效地这样说:
Part of _.template
's internals involves calling String#replace
on 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(undefined)
如果没有 #tmpl_sourcelist
的时候你说DOM $('#tmpl_sourcelist')。HTML()可能发生code>。
That can happen if there is no #tmpl_sourcelist
in the DOM when you say $('#tmpl_sourcelist').html()
.
有几个简单的解决方案:
There are a few simple solutions:
- 调整你的
&LT;脚本&GT;
为了让你的#tmpl_sourcelist
来尝试加载你的看法了。 -
创建编译模板函数视图的
初始化
而不是在视图中的类的定义:
- Adjust your
<script>
order so that your#tmpl_sourcelist
comes before you try to load your view. Create the compiled template function in your view's
initialize
instead of in the view's "class" definition:
window.SourceListView = Backbone.View.extend({
tagName:"li",
initialize:function () {
this.template = _.template($('#tmpl_sourcelist').html());
//...
至于标签名
得好,精细的手工有这个说:
报 view.el
[...] this.el
从创建视图的标签名
,的className
, ID
和属性
属性,如果指定。如果不是,报是一个空 DIV
。
[...] this.el
is created from the view's tagName
, className
, id
and attributes
properties, if specified. If not, el is an empty div
.
所以,有这个在您的视图:
So having this in your view:
tagName: 'li'
意味着骨干会自动创建一个新的&LT;李方式&gt;
元素作为视图的报
这篇关于未捕获类型错误:不能调用方法'取代'Backbone.js的未定义的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!