Javascript 主干未捕获类型错误:字符串不是函数
这些天在学习英语。下面是我的错误:我真的不明白哪里出了问题,所以请查看下面的错误,并建议我对代码进行必要的更改Javascript 主干未捕获类型错误:字符串不是函数,javascript,jquery,backbone.js,Javascript,Jquery,Backbone.js,这些天在学习英语。下面是我的错误:我真的不明白哪里出了问题,所以请查看下面的错误,并建议我对代码进行必要的更改 Uncaught TypeError: string is not a function wines.html:56 app.WineView.Backbone.View.extend.render wines.html:56 app.WinesListView.Backbone.View.extend.addOne wines.html:73 (anonymous function)
Uncaught TypeError: string is not a function wines.html:56
app.WineView.Backbone.View.extend.render wines.html:56
app.WinesListView.Backbone.View.extend.addOne wines.html:73
(anonymous function) underscore.js:70
h.each.h.forEach underscore.js:103
g.(anonymous function) backbone.js:966
app.WinesListView.Backbone.View.extend.addAll wines.html:69
f backbone.js:208
e.Events.trigger backbone.js:148
i.extend.reset backbone.js:770
t.success backbone.js:865
j jquery.js:3073
k.fireWith jquery.js:3185
x jquery.js:8251
(anonymous function)
这是我的代码:
<div id="header">
<input type="button" value="New Wine" />
</div>
<div id="sidebar">
<ul class="wines-list"></ul>
</div>
<div id="content">
</div>
<script type="text/template" id="wine">
<a href="#wines/<%= id %>">
<%=n ame %>
</a>
</script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.7.0/underscore-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js"></script>
<script type="text/javascript">
var app = {}
app.Wine = Backbone.Model.extend({
urlRoot: "api/wines",
defaults: {
"id": null,
"name": "",
"grapes": "",
"country": "USA",
"region": "California",
"year": "",
"description": "",
"picture": ""
}
});
app.WinesList = Backbone.Collection.extend({
model: app.Wine,
url: 'api/wines'
});
app.winesList = new app.WinesList();
app.WineView = Backbone.View.extend({
tagName: 'li',
template: $('#wine').html(),
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
app.WinesListView = Backbone.View.extend({
el: '#sidebar',
initialize: function() {
this.listenTo(app.winesList, 'reset', this.addAll);
app.winesList.fetch({
reset: true
});
},
addAll: function() {
app.winesList.forEach(this.addOne, this);
},
addOne: function(wine) {
wineView = new app.WineView({
model: wine
});
$('.wines-list').append(wineView.render().el);
}
});
app.winesListView = new app.WinesListView();
</script>
var app={}
app.Wine=Backbone.Model.extend({
urlRoot:“api/葡萄酒”,
默认值:{
“id”:空,
“名称”:“,
“葡萄”:“,
“国家”:“美国”,
“地区”:“加利福尼亚”,
“年”:“年”,
“说明”:“,
“图片”:”
}
});
app.WinesList=Backbone.Collection.extend({
型号:app.Wine,
url:“api/葡萄酒”
});
app.winesList=新建app.winesList();
app.WineView=Backbone.View.extend({
标记名:“li”,
模板:$('#wine').html(),
render:function(){
this.el.html(this.template(this.model.toJSON());
归还这个;
}
});
app.WinesListView=Backbone.View.extend({
el:“#侧边栏”,
初始化:函数(){
this.listenTo(app.winesList,'reset',this.addAll);
app.winesList.fetch({
重置:正确
});
},
addAll:function(){
app.winesList.forEach(this.addOne,this);
},
addOne:功能(葡萄酒){
wineView=新应用程序。wineView({
型号:葡萄酒
});
$('.wines list').append(wineView.render().el);
}
});
app.winesListView=新建app.winesListView();
我已经在google中搜索了这个错误,但在我的例子中没有理解。在您的
wineView
中,模板属性引用了一个元素的内容(因此在本例中是一个字符串),然后您尝试将该元素用作函数(因此出现错误)
尝试将元素传递给下划线模板函数,然后在渲染中使用该函数传递模型
app.WineView = Backbone.View.extend({
tagName: 'li',
template: _.template($('#wine').html()),
render: function() {
this.$el.html(this.template(this.model.toJSON()));
return this;
}
});
谢谢我忘了放u。template这个答案没有多大意义,因为您已经从我引用的问题中删除了部分代码。对于事后阅读此内容的人来说,如果他们无法识别原始错误,则此内容将不再有用。