使用带骨干车把 [英] Using Handlebars with Backbone
问题描述
我学习骨干/把手/要求。我到处都找过网上和SO - 在那里,你可以直接我将提供有用的信息使用,而不是使用下划线车把任何教程或网站
I am learning Backbone/Handlebars/Require. I have looked all over online and on SO - are there any tutorials or websites that you can direct me to that would provide helpful information for using using handlebars instead of underscore?
推荐答案
使用handlebars.js而不是下划线模板是pretty简单。看看这个例子:
Using handlebars.js instead of underscore templating is pretty straightforward. Check out this example:
https://cdnjs.com/libraries/backbone。 JS /教程/什么,是一个视图
(滚动到载入模板部分)
https://cdnjs.com/libraries/backbone.js/tutorials/what-is-a-view (scroll to the "Loading a Template" section)
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 );
}
});
基本上,骨干惯例是建立你的HTML渲染功能。使用模板引擎留给完全取决于你(我喜欢骨干)。所以,你只希望将其更改为...
Basically, the convention in backbone is to build your html in a render function. The use of templating engine is left completely up to you (which I like about Backbone). So you'd just change it to...
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,你可以在你的模块的顶部做把手的依赖。我是pretty新本,但它听起来就像是学习重点将是Backbone.js的模式和require.js使用。
Since you're using require.js, you can make Handlebars a dependency at the top of your module. I'm pretty new to this, but it sounds like the learning to focus on would be Backbone.js patterns and require.js usage.
这篇关于使用带骨干车把的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!