javascript 使用主干js加载模板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14097535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading templates with backbone js
提问by Goofyahead
I'm starting in javascript development, and did a simple project with node.js as a rest API and a client using backbone, everything look perfectly till I want to get my templates out of my js.
我开始从事 javascript 开发,并使用 node.js 作为休息 API 和使用主干的客户端做了一个简单的项目,一切看起来都很完美,直到我想从我的 js 中获取我的模板。
I found different approaches, some of them with some time (like one year old) but I can't understand which one could be better:
我找到了不同的方法,其中一些需要一些时间(比如一岁),但我不明白哪个更好:
A .js file with a var with the html code
pros -> easy to load, easy to pass to underscore to compile it.
cons -> scape every single line.
app.templates.view = " \ <h3>something code</h3> \ ";
load template:
template: _.template(app.templates.view)
带有 html 代码的 var 的 .js 文件
优点 -> 易于加载,易于传递给下划线进行编译。
缺点 -> 对每一行进行转义。
app.templates.view = " \ <h3>something code</h3> \ ";
加载模板:
template: _.template(app.templates.view)
External template in Underscore
Use require.js to load with text plug-in.
pros -> load different templates as needed.
cons -> I don't like the approach to put everything inside a "loader" function...
define(["TemplateEngine", "text!templates/template.html"], function(...
使用 require.js 加载文本插件。
优点 -> 根据需要加载不同的模板。
缺点 -> 我不喜欢把所有东西都放在“加载器”函数中的方法......
define(["TemplateEngine", "text!templates/template.html"], function(...
RequireJS: Loading modules including templates and CSS
A function that loads the templates with an AJAX petition.
pros -> You can load the template that you need and adds local storage posibilities.
cons -> Seems that I have to merge all my templates into one file for production environments.
function() { var templateLoader = {... $.get calls ...}
使用 AJAX 请求加载模板的函数。
优点 -> 您可以加载所需的模板并添加本地存储可能性。
缺点 -> 似乎我必须将所有模板合并到一个文件中以用于生产环境。
function() { var templateLoader = {... $.get calls ...}
Best way to asynchronously load underscore templates
- And a Jquery plug-in for template loading that I really liked but it seems that it didn't go to release?
- 还有一个我很喜欢的模板加载Jquery插件,但是好像没有去发布?
http://api.jquery.com/jQuery.template/
http://api.jquery.com/jQuery.template/
It seems that require is the best approach, but maybe I'm missing something, I do wan't to make things as clean as possible since I'm in the learning/having fun phase :D
似乎 require 是最好的方法,但也许我错过了一些东西,我不想让事情尽可能干净,因为我正处于学习/玩乐阶段:D
Any good article or github project with a good structure or any light on this will be appreciated.
任何具有良好结构或对此有任何了解的好文章或 github 项目将不胜感激。
Thanks.
谢谢。
Excuse any major spelling mistake, not an English speaker :)
请原谅任何主要的拼写错误,而不是说英语的人:)
--EDIT-- found some interesting videos to understand how to start and wrap things with require.js http://www.youtube.com/watch?v=VGlDR1QiV3A
--EDIT-- 找到了一些有趣的视频来了解如何使用 require.js http://www.youtube.com/watch?v=VGlDR1QiV3A开始和包装东西
采纳答案by hunterloftis
I would recommend using require.js with text plugin. Mixing html templates as strings in javascript variable is bad idea, as well as using something like <script type="text/template"></script>
.
我建议使用 require.js 和文本插件。在 javascript 变量中混合 html 模板作为字符串是个坏主意,以及使用类似<script type="text/template"></script>
.
Here is one very good series on backbone.js which covers template loading and project build as well: http://dailyjs.com/2012/11/29/backbone-tutorial-1/. Github project is also provided there.
这是关于backbone.js 的一个非常好的系列,它也涵盖了模板加载和项目构建:http: //dailyjs.com/2012/11/29/backbone-tutorial-1/。那里也提供了 Github 项目。
回答by hunterloftis
Require is a good option from the ones you listed.
Require 是您列出的一个不错的选择。
Is there a reason you haven't considered simply:
有没有你没有简单考虑过的原因:
Storing templates in the pages that use them as
<script type='text/template'>
nodes?Storing templates as text (non-JS) files and loading them via XHR on pages that use them?
将模板存储在使用它们作为
<script type='text/template'>
节点的页面中?将模板存储为文本(非 JS)文件并通过 XHR 在使用它们的页面上加载它们?