使用带有 Backbone 的车把
我正在学习 Backbone/Handlebars/Require。我已经在网上和 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
handlebars.js
而不是underscore
模板非常简单。查看此示例:https://cdnjs.com/libraries /backbone.js/tutorials/what-is-a-view
(滚动到“加载模板”部分)
基本上,backbone 中的约定是在渲染函数中构建 html。模板引擎的使用完全由您决定(我喜欢 Backbone)。因此,您只需将其更改为:
由于您使用的是
require.js
,因此您可以将Handlebars
作为模块顶部的依赖项。我对此还很陌生,但听起来要重点学习的是Backbone.js
模式和require.js
用法。Using
handlebars.js
instead ofunderscore
templating is pretty straightforward. Check out this example:https://cdnjs.com/libraries/backbone.js/tutorials/what-is-a-view
(scroll to the "Loading a Template" section)
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:
Since you're using
require.js
, you can makeHandlebars
a dependency at the top of your module. I'm pretty new to this, but it sounds like the learning to focus on would beBackbone.js
patterns andrequire.js
usage.我更愿意编译模板一次(在初始化期间),这样就可以避免每次渲染时都重新编译模板。此外,您需要将模型传递给编译模板才能生成 HTML:
I would prefer to compile the template once (during initialize), that way you avoid to recompile the template with every render. Also, you need to pass the model to the compilated template in order to generate the HTML:
如果您使用 require.js,您将无法使用当前的 Handlebars 文件。我使用了以下 Handlebars 插件,它似乎与当前版本保持同步。如果 Handlebars 在您的模块中返回 null,只需将您的 Handlebars 文件替换为上面的插件即可。
If you are using require.js you wont be able to use the current Handlebars file. I used the following Handlebars Plugin and it seems to be kept up to date with the current version. Just replace your Handlebars file with the plugin above if Handlebars is returning null in your module.