与骨干JS装载模板 [英] Loading templates with backbone js
问题描述
我开始在JavaScript的开发,并利用骨干,一切看起来完美的,直到我想我的模板,从我的JS做的是Node.js作为一个REST API和一个客户端一个简单的项目。
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.
我发现不同的方法,有的还带有一定的时间(比如一岁),但我不明白哪一个会更好:
I found different approaches, some of them with some time (like one year old) but I can't understand which one could be better:
-
一个.js文件与html的code进行VAR
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> \
";
负载模板:
template: _.template(app.templates.view)
在下划线外部模板
-
使用require.js加载文本插件。
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(...
<一个href="http://stackoverflow.com/questions/7917639/requirejs-loading-modules-including-templates-and-css">RequireJS:加载模块,包括模板和CSS
-
一个函数加载模板与AJAX请愿书。
A function that loads the templates with an AJAX petition.
利弊 - >您可以加载你所需要的模板,并添加本地存储posibilities
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 ...}
<一个href="http://stackoverflow.com/questions/7542089/best-way-to-load-asynchronously-undescore-templates">Best方式来加载异步undescore模板
- 和一个jQuery插件,我真的很喜欢,但它似乎没有去释放?模板加载
http://api.jquery.com/jQuery.template/
看来需要是最好的方法,但也许我失去了一些东西,我不wan't让事情尽可能的干净,因为我在学习/玩乐阶段:D
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
在这个任何好的文章或github上的项目具有良好的结构或任何光线将AP preciated。
Any good article or github project with a good structure or any light on this will be appreciated.
感谢。
请问什么大的拼写错误,不说英语的人:)
Excuse any major spelling mistake, not an English speaker :)
- EDIT-- 发现了一些有趣的视频,以了解如何启动和换东西与require.js http://www.youtube.com/watch?v=VGlDR1QiV3A
--EDIT-- found some interesting videos to understand how to start and wrap things with require.js http://www.youtube.com/watch?v=VGlDR1QiV3A
http://www.youtube.com/watch?v=M-wjQjsryMY
推荐答案
我会建议使用require.js与文字插件。混合HTML模板在JavaScript变量是坏主意字符串,以及使用类似&LT;脚本类型=文/模板&GT;&LT; / SCRIPT&GT;
。
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>
.
下面是Backbone.js的一款非常好的系列,涵盖模板加载和项目建设,以及:的 http://dailyjs.com/2012/11/29/backbone-tutorial-1/ 。 Github上项目还提供了那里。
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.
这篇关于与骨干JS装载模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!