javascript 未捕获的类型错误:无法调用未定义 underscore.js 的方法“替换”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18994799/
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 underscore.js
提问by titi
I'm newbie to backbone.js
and underscore.js
.
我是backbone.js
和 的新手underscore.js
。
HTML :
HTML :
<div id="cartlist">
<script type="text/template" id="cart_template">
</script>
</div>
Where I called the view file :
我在哪里调用视图文件:
<script type="text/javascript" src="webcore/views/CartView.js"></script>
</body>
JS function(it works well with the javascript project):
JS 函数(它适用于 javascript 项目):
function Cart(){
......
this.showCart = function (){
var item = deserializeJSONToObj(window.localStorage.getItem(Cart.storageName));
var str = '<table id="showcart">';
str += '<tr><td class="cartitemhead">Item to buy</td><td class="cartitemhead" style="text-align: center;">Quantity</td></tr>';
$.each(item, function(i, item) {
str += '<tr><td><table class="verticallist"><tr><td rowspan="4" style="width: 120px;"><img src="' + item.PictureName + '" alt="Product" width="95px"/></td><td style="font-weight: bold;">'+trimString(item.Name,50)+'</td></tr><tr><td><i>Available in Stock(s)!</i></td></tr><tr><td><i>Rating: 650Va-390w Input: Single</i></td></tr></table></td><td class="centertxt">'+item.QuantityInCart+'</td></tr>';
});
str += '</table>';
return str;
}
This is the Views :
这是意见:
var myCart = new Cart();
CartList = Backbone.View.extend({
initialize:function(){
this.render();
},
render: function(){
var template = _.template( $("#cart_template").html(), {} );
this.$el.html( template );
}
});
var cart_view = new CartList({ el: $("#cartlist").html(myCart.showCart()) });
when I trying to call the view template, I am getting error
Uncaught TypeError: Cannot call method 'replace' of undefined - underscore.js
. Please help me to find the mistake.How can I convert
str
string inCart
class to atemplate
ofunderscore.js
.
当我尝试调用视图模板时,出现错误
Uncaught TypeError: Cannot call method 'replace' of undefined - underscore.js
。请帮我找出错误。如何将类中的
str
字符串转换为.Cart
template
underscore.js
Any help would be much appreciated, thank you.
任何帮助将不胜感激,谢谢。
回答by mu is too short
I'd guess that your problem is where you're storing #cart_template
in the DOM:
我猜你的问题是你#cart_template
在 DOM 中存储的位置:
<div id="cartlist">
<script type="text/template" id="cart_template">
</script>
</div>
You create your CartList
like this:
你创建你CartList
这样的:
var cart_view = new CartList({ el: $("#cartlist").html(myCart.showCart()) });
When you say this:
当你这样说时:
$("#cartlist").html(myCart.showCart())
everything that was inside #cartlist
is gone, in particular, #cart_template
is gone. Then inside CartList
, you try to do this:
里面的一切都#cartlist
消失了,尤其#cart_template
是消失了。然后在里面CartList
,你尝试这样做:
_.template( $("#cart_template").html(), {} );
But there is no more #cart_template
at that point so $('#cart_template').html()
is undefined
and that's where your error comes from: _.template
will call replace
internally but you're giving it undefined
as the template instead of a string.
但没有更多的#cart_template
在这一点所以$('#cart_template').html()
是undefined
和您的错误来自于的:_.template
将调用replace
内部,但你给它undefined
作为模板,而不是一个字符串。
The solution is to move your #cart_template
outside #cartlist
.
解决办法是移动你的#cart_template
外部#cartlist
。
I'd also recommend that you not pass the el
to the view's constructor, your view should create its own el
and the caller should put that el
where it wants it in the DOM. That way the view is wholly responsible for its element and you'll have fewer zombie and event problems.
我还建议您不要将 传递el
给视图的构造函数,您的视图应该创建自己的视图el
,并且调用者应该将el
它放在 DOM 中它想要的位置。这样,视图对其元素完全负责,并且您将有更少的僵尸和事件问题。