[转载]Require.js Example – Setup Time 2 Minutes
http://www.sitepoint.com/require-js-setup-time-2-minutes/
Setup Require.js in just 2 minutes. or download the code below and have it already working.
What is Require.js?
RequireJS is a JavaScript file and module loader. It is optimized for in-browser use, but it can be used in other JavaScript environments, like Rhino and Node. Using a modular script loader like RequireJS will improve the speed and quality of your code.
- Speed – Asynchronous JavaScript Loading.
- Manage JavaScript dependencies such as jQuery plugins.
- File Structure your web app files.
- When you get good you can code modules which do specific web app stuff.
- Removes the need for including 100 script tags in your HTML.
- Can be easily integrate with build scripts.
Does it work?
Yes. Screenshot below was taken from my dev with chrome dev tools open (disabling cache) so is naturally fast but amazingly even here you can see a performance increase.


Web App Scructure
This is a very basic structure you can use for a web app.
- root/
- index.html
- js
- vendor
- [External JavaScript Files & jQuery Plugins]
- app
- main.js
- [your modules and web app JavaScript files]
- app.js
- vendor
- css
- img
HTML Before:
The normal way to load scripts… modernizr goes in the head, the rest in the body.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< !DOCTYPE html><head> <title>My Web App</title> <link rel="stylesheet" href="app/css/main.css"/> <script src="app/js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script></head><body> <div id="main" class="container"></div> <script src="app/js/vendor/jquery-1.9.1.min.js"></script> <script src="app/js/vendor/underscore.min.js"></script> <script src="app/js/vendor/backbone.min.js"></script> <script src="app/js/vendor/bootstrap.min.js"></script> <script src="app/js/main.js"></script></body> |
HTML After:
Require.js goes in the head. Nice and neat.
|
1
2
3
4
5
6
7
8
9
10
11
|
< !DOCTYPE html><head> <title>My Web App</title> <link rel="stylesheet" href="app/css/main.css"/> <script data-main="js/app" src="js/vendor/require.js"></script></head><body> <div id="main" class="container"></div></body> |
app.js
This file contains the config for require.js. If you change the directory structure this needs to match. I’m showing you the shim version, you can also load jQuery from a CDN.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// Place third party dependencies in the lib folder//// Configure loading modules from the lib directory,requirejs.config({ "baseUrl": "js/vendor", "paths": { "app": "../app" }, "shim": { "backbone": ["jquery", "underscore"], "bootstrap": ["jquery"] }});// Load the main app module to start the apprequirejs(["app/main"]); |
main.js
This file contains the web app dependencies and once loaded you can start your app using whatever framework you like such as Backbone or Angular.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
//Load Web App JavaScript Dependencies/Pluginsdefine([ "jquery", "modernizr", "underscore", "backbone", "bootstrap"], function($){ $(function() { //do stuff console.log('required plugins loaded...'); });}); |
Still can’t get it working?
[转载]Require.js Example – Setup Time 2 Minutes的更多相关文章
- require.js学习笔记(内容属于转载总结)
<script data-main="src/app" src="src/lib/require.js"></script> backb ...
- 转载 r.js打包经验
例子1 先是HTML页面 <!DOCTYPE html> <html> <head> <title>My App</tit ...
- Javascript模块化编程(三):require.js的用法 作者: 阮一峰
声明:转载自阮一峰的网络日志 这个系列的第一部分和第二部分,介绍了Javascript模块原型和理论概念,今天介绍如何将它们用于实战. 我采用的是一个非常流行的库require.js. 一.为什么要用 ...
- js模块化开发——require.js的用法详细介绍(含jsonp)
RequireJS的目标是鼓励代码的模块化,它使用了不同于传统<script>标签脚本加载步骤.可以用它回事.优化代码,但其主要的目的还是为了代码的模块化.它鼓励在使用脚本以moudle ...
- require.js模块化写法
模块化 模块就是实现特定功能的一组方法.只要把不同的函数(以及记录状态的变量)简单地放在一起,就算是一个模块. 下述两种写法等价 exports 对象是当前模块的导出对象,用于导出模块公有方法和属性. ...
- javascript模块化编程库require.js的用法
随着javascript的兴起,越来越多的公司开始将JS模块化,以增加开发的效率和减少重复编写代码的.更是为了能更加容易的维护日后的代码,因为现在的随着人们对交互效果的越来越强烈的需求,我们的JS代码 ...
- jQuery 对AMD的支持(Require.js中如何使用jQuery)
AMD 模块 AMD(异步模块定义,Asynchronous Module Definition)格式总体的目标是为现在的开发者提供一个可用的模块化 JavaScript 的解决方案. AMD 模块格 ...
- require.js 简洁入门
原文地址:http://blog.sae.sina.com.cn/archives/4382 前言 提到require.js大多数人会说提到模块化开发,AMD等等,其实require.js并没有这么多 ...
- require.js 最佳实践【转】
https://www.cnblogs.com/digdeep/p/4607131.html require.js是一个js库,相关的基础知识,前面转载了两篇博文:Javascript模块化编程(re ...
随机推荐
- String 类 常用函数
构造方法摘要: String(byte[] bytes) 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String. String(char[] value) ...
- 对象字典0x1005和0x1006的理解
SYNC不一定由主站产生,因此,产生SYNC的节点,0x1005对象的值一般是0x40000080,第30位为1表示本节点产生 SYNC,而本节点的0x1006对象就是产生同步周期值了;而接收SYNC ...
- rpm安装和二进制安装
rpm包安装 Tomcat RPM安装(先安装JDK + 再安装Tomcat) 1:升级系统自带的JDK(也可以使用oracle的JDK) yum install -y java-1.8.0-open ...
- 10.13课堂Scrum站立会议
项目名称:C#实现的连连看游戏 小组名称:计信F4 开会时间 :2016年10月11日 20:20~20:40 组长:张政 成员:张金生,武志远,李泉 内容: 昨日已完成: 张政:构建基础逻辑,实现游 ...
- [转]string和stringstream用法总结
转自:http://blog.csdn.net/xw20084898/article/details/21939811 作者:xw20084898 一.string string 是 C++ 提供的字 ...
- [转帖]一文读懂 HTTP/2
一文读懂 HTTP/2 http://support.upyun.com/hc/kb/article/1048799/ 又小拍 • 发表于:2017年05月18日 15:34:45 • 更新于:201 ...
- SQL语句查询一个数据库中的所有表
--读取库中的所有表名 select name from sysobjects where xtype='u' --读取指定表的所有列名 select name from syscolumns whe ...
- Java并发编程中的设计模式解析(一)
Java并发编程,除了被用于各种Web应用.分布式系统和大数据系统,构成高并发系统的核心基础外,其本身也蕴含着大量的设计模式思想在里面.这一系列文章主要是结合Java源码,对并发编程中使用到的.实现的 ...
- mysql时间函数和时间操作
补 原文链接:http://blog.csdn.net/yuxiayiji/article/details/7480785 select timediff('23:40:00', ' 18:30:00 ...
- 【刷题】UOJ #274 【清华集训2016】温暖会指引我们前行
寒冬又一次肆虐了北国大地 无情的北风穿透了人们御寒的衣物 可怜虫们在冬夜中发出无助的哀嚎 "冻死宝宝了!" 这时 远处的天边出现了一位火焰之神 "我将赐予你们温暖和希望! ...