[英]Set lodash / underscore template settings globally using require.js
[英]Using text.js with require.js how can I create one template html file but grab each script template individually?
我正在使用require.js和text.js加载其中包含一堆<script>
模板的模板文件:
例如/scripts/templates/comments.html
<script type="text/template" id="js-comment-reply-tmpl">
// html in here
</script>
<script type="text/template" id="js-comment-edit-tmpl">
// html in here
</script>
并且由于使用下划线的模板系统(或任何类似的js微模板系统),因此该文件本身将作为字符串加载。 是否有一种聪明的方法可以只从该文件中获取每个模板? 例如$(template).html()
将其包装在jQuery中,然后在其上执行find()
或其他操作? 不过,我基本上必须首先将其放入DOM中,这样可能会变得很慢,而且我甚至不加载text.js并将其最初从DOM中拔出。
我的另一种想法是将它们分别分成各自的文件,但这会减慢请求时间(尽管我可能最终会使用带有节点的r.js最终将其全部缩小,因此不会)物)。
例如/scripts/templates/comment_reply.html
<script type="text/template" id="js-comment-reply-tmpl">
// html in here
</script>
例如/scripts/templates/comment_edit.html //此处的html
最好/最有效的方法是什么?
我建议将每个模板移到它自己的文件中,然后分别加载它们。 这样做的一些优点是:
维护更容易 -如果项目模板都有自己的名称,则在项目中搜索模板会更容易,并且如果开发人员并非都在编辑同一文件,则还可以减少源代码管理冲突的麻烦。
可移植性 -如果最终使用的是可在服务器上使用的模板系统(如Mustache),则模板已经很容易在前端和后端之间共享。
您已经强调的主要缺点是额外的请求,但是绝对不要在不使用r.js构建脚本的情况下投入生产,所以这应该不是问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.