Javascript 主干js,外部模板,不带Requirejs
我正在尝试学习Backbone.js外部模板,而不使用Require.js。但是模板没有显示,div容器仍然是空的,尽管我没有发现任何错误。以下是我正在编写的代码(我已经包括了它们的路径,以防万一):Javascript 主干js,外部模板,不带Requirejs,javascript,jquery,node.js,backbone.js,underscore.js,Javascript,Jquery,Node.js,Backbone.js,Underscore.js,我正在尝试学习Backbone.js外部模板,而不使用Require.js。但是模板没有显示,div容器仍然是空的,尽管我没有发现任何错误。以下是我正在编写的代码(我已经包括了它们的路径,以防万一): scripts/main.js,用作我的路由器 var AppRouter = Backbone.Router.extend({ routes: { "profile": "profile", "view": "view", "else": "notFound" }, p
scripts/main.js
,用作我的路由器
var AppRouter = Backbone.Router.extend({
routes: {
"profile": "profile",
"view": "view",
"else": "notFound"
},
profile: function() {
if (!this.profileView) {
this.profileView = new ProfileView();
}
$('#global-content').html(this.profileView.el);
}
});
utils.loadTpl([
'ProfileView'],
function() {
app = new AppRouter();
Backbone.history.start();
}
);
scripts/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);
}
};
scripts/views/profile.js
,视图也有相同的代码
window.ProfileView = Backbone.View.extend({
initialize: function() {
this.render();
},
render: function() {
$(this.el).html(this.template);
return this;
}
});
然后我将模板放在与scripts/
我试着在控制台中运行它们,如果有帮助的话,它们工作得很好
profileView=newprofileview()render()代码>
profileView.el
——它正确地提供了profile.html模板
我读过关于Require.js的文章,但我不想用它
谢谢请检查这行
$(this.el).html(this.template);
它应该被替换为
$(this.el).html(this.template());
但如果你说这是可行的,我想这可能是另一个问题
profileView = new ProfileView().render();
console.log(profileView.el);
一切正常
问题出在index.html
中,
我用了
但是在main.js
中,我要求#全球内容主干推荐感谢您的建议,但我使用的是yeoman
,我只想基于github report来做。谢谢您的回答,虽然它没有解决我的问题,但我找到了答案,因为它。您会发布答案吗?很抱歉,我只是在放置代码(最后一部分)以替换main.js中的profile:function(){..}
时才注意到它,因为它肯定会在控制台中输出模板。谢谢