Javascript 主干js缺失方法';模板&x27;
我在主干js中练习使用外部模板,我得到了一个类型错误,它说对象没有Javascript 主干js缺失方法';模板&x27;,javascript,backbone.js,Javascript,Backbone.js,我在主干js中练习使用外部模板,我得到了一个类型错误,它说对象没有模板 此外,如果路由器位于另一条路径,路由器如何识别/调用视图,反之亦然 我已经包括了我正在研究的代码: profile.js window.ProfileView = Backbone.View.extend({ initialize: function() { this.render(); }, render: function() { $(this.el).html(this.template());
模板
此外,如果路由器位于另一条路径,路由器如何识别/调用视图,反之亦然
我已经包括了我正在研究的代码:
profile.js
window.ProfileView = Backbone.View.extend({
initialize: function() {
this.render();
},
render: function() {
$(this.el).html(this.template());
return this;
}
});
main.js
var AppRouter = Backbone.Router.extend({
routes: {
'profile' : 'profile'
},
profile: function() {
this.profileView = new ProfileView();
$('#global-content').html(this.profileView.el);
}
});
utils.loadTpl (['profile'], function() {
appRouter = new AppRouter();
Backbone.history.start();
});
utils.js
window.utils = {
loadTpl: function(views, callback) {
var deferreds = [];
$.each(views, function(index, view) {
if (window[view]) {
deferreds.push($.get('templates/' + view + '.html', function(data) {
window[view].prototype.template = _.template(data);
}));
} else {
// alert(view + " not found");
}
});
$.when.apply(null, deferreds).done(callback);
}
};
模板不是函数,您需要将其作为视图的属性调用,以便删除
()
e、 g
是的,它消除了错误,但是模板仍然没有显示你是如何设置模板的,在你的代码中你不是很明显。通常我使用require.js检索资源,然后在视图中专门设置模板,例如
template:template,
,它本身在utils.js中指定。我故意不使用require.js来引用。不幸的是,我不熟悉这个方法,我总是使用require.js。
$(this.el).html(this.template);