我正在学习 Backbone/Handlebars/Require。我已经在网上和 SO 上浏览过 - 有没有任何教程或网站可以指导我访问,以提供使用 Handlebars 而不是下划线的有用信息?
最佳答案
使用 handlebars.js
而不是 underscore
模板非常简单。看看这个例子:
https://cdnjs.com/libraries/backbone.js/tutorials/what-is-a-view (滚动到“加载模板”部分)
SearchView = Backbone.View.extend({
initialize: function(){
this.render();
},
render: function(){
// Compile the template using underscore
var template = _.template( $("#search_template").html(), {} );
// Load the compiled HTML into the Backbone "el"
this.el.html( template );
}
});
基本上,backbone 中的约定是在渲染函数中构建 html。模板引擎的使用完全由您决定(我喜欢 Backbone)。所以你只需将其更改为:
SearchView = Backbone.View.extend({
initialize: function(){
this.render();
},
render: function(){
// Compile the template using Handlebars
var template = Handlebars.compile( $("#search_template").html() );
// Load the compiled HTML into the Backbone "el"
this.el.html( template );
}
});
由于您使用的是 require.js
,因此您可以将 Handlebars
设为模块顶部的依赖项。我对此还很陌生,但听起来要重点学习的是 Backbone.js
模式和 require.js
用法。
关于backbone.js - 使用带有 Backbone 的 Handlebars ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9072324/