我有一个骨干应用程序,我希望在触发时显示视图。当我点击锚点时控制台说:Uncaught TypeError: Cannot read property 'fn' of undefined
- 我检查了其他类似的问题,他们说jQuery没有加载?跆拳道?我不明白......
我的菜单视图:
var LeftMenuView = Backbone.View.extend({
template: Handlebars.compile(Template),
events: {
'click li a.artAll': 'artAll',
},
artAll: function(event) {
event.preventDefault();
var artAllRouter = new Backbone.Router();
var route = '/artists/top100/all';
artAllRouter.navigate(route, {trigger: true});
}
....
控制台告诉我错误源于handlebars.js
?
我的HTML模板:
<div>
{{#each}}
<p>
<a href="#">{{artist_name}}</a>
</p>
{{/each}}
</div>
和我的观点:
define(['backbone','handlebars', 'text!templates/TopAllArtists.html'],
function(Backbone,Handlebars, Template) {
var TopAllArtView = Backbone.View.extend({
template: Handlebars.compile(Template),
initialize: function () {
_.bindAll(this, 'render');
},
render: function() {
var self = this;
self.collection.each(function(model){
self.$el.append(self.template({
artist_name:model.get('artist_name')
})
);
});
return this;
}
});
return TopAllArtView;
}
);
任何人都知道可能是什么问题?
答案 0 :(得分:4)
根据Handlebars documentation,each
需要一个列表来迭代,
<ul class="people_list">
{{#each people}}
<li>{{this}}</li>
{{/each}}
</ul>
艺术家的名字是否可以迭代?
如果model.get('artist_name')
可迭代,['name1', 'name2', ...]
比,
<div>
{{#each artist_name}}
<p>
<a href="#">{{this}}</a>
</p>
{{/each}}
</div>
如果model.get('artist_name')
是字符串,'Johann Sebastian Bach'
比,
<div>
<p>
<a href="#">{{artist_name}}</a>
</p>
</div>