RequireJs调研
背景
Problem(问题)
- Web sites are turning into Web apps(网站正转变为网络应用程序)
- Code complexity grows as the site gets bigger(代码复杂度随着站点变大而变复杂)
- Assembly gets harder(组装变得更难【ps】这里我个人认为“组装”是拼接单个js文件中的昂多的代码段 )
- Developer wants discrete JS files/modules(开发者想分离js文件/模块)
- Deployment wants optimized code in just one or a few HTTP calls(网站部署者想通过使用一个或者很少http请求来优化代码)
Solution(解决方案)
- Front-end developers need a solution with:(前端工程师需要一个解决方案,拥有这些功能:)
- Some sort of #include/import/require(一些引入文件的命令语句)
- ability to load nested dependencies(加载嵌套的依赖文件)
- ease of use for developer but then backed by an optimization tool that helps deployment(简单好用,但也有助于优化部署)
RequireJs简介
作用
RequireJS的目标是鼓励代码的模块化,它使用了不同于传统script标签的脚本加载步骤。可以用它来加速、优化代码,但其主要目的还是为了代码的模块化。它鼓励在使用脚本时以module ID替代URL地址。
优点
速度快

依赖关系清晰

requirejs通过a.js调用b.js,b.js调用c.js,c.js调用d.js的原理,让我们可以只调用a.js,就可以加载所有的js,而且依赖关系非常清晰。
js文件不仅可以调用js,还能调用css和html页面,所以毫不夸张地说,引入一个入口js,无需向 HTML 文件添加任何其他元素即可构建大型单页面应用程序
鼓励代码模块化(最大优点)
使用requirejs,我们就需要将原来在一个js文件里面写的代码,按照模块解耦分离成多个js文件,然后按照需求调用。这样做的好处很多:
解耦了,就不会出现牵一发动全身的情况,便于日后维护;还能便于多人分工合作;还能复用...
在项目中使用RequireJs
调用第三方插件
使用多选插件
require(['jquery.shiftcheckbox'], function () {
$(function () {
$('#demo2 :checkbox').shiftcheckbox();
})
})
使用'bootstrap', 'jquery.form', 'jquery.validate'三个插件
require(['bootstrap', 'jquery.form', 'jquery.validate'], function () {
$(function () {
jQuery.validator.addMethod("isZipCode", function (value, element) {
var tel = /^[0-9]{6}$/;
return this.optional(element) || (tel.test(value));
}, "请正确填写您的邮政编码");
$("#signupForm").validate({
rules: {
firstname: {
required: true
},
email: {
required: true,
email: true
},
password: {
required: true,
minlength: 5
},
confirm_password: {
required: true,
minlength: 5,
equalTo: "#password"
},
isZipCode: {
isZipCode: true
}
},
messages: {
firstname: "请输入姓名",
email: {
required: "请输入Email地址",
email: "请输入正确的email地址"
},
password: {
required: "请输入密码",
minlength: jQuery.format("密码不能小于{0}个字 符")
},
confirm_password: {
required: "请输入确认密码",
minlength: "确认密码不能小于5个字符",
equalTo: "两次输入密码不一致不一致"
}
}
});
$("#signupForm").ajaxSubmit();
});
});
调用自己写的方法或者类
调用日期方法类
/**
* Created by lewis on 15-9-15.
*/
//自定义的命名空间,用来对日期进行操作
define([],{
//输入json日期获取年
getYear: function (jsonDate) {
var dateArry = jsonDate.split('-');
var jsonyear = parseInt(dateArry[0]);
return jsonyear;
},
//输入json日期获取月
getMonth: function (jsonDate) {
var dateArry = jsonDate.split('-');
var jsonmonth = parseInt(dateArry[1]);
return jsonmonth;
},
//输入json日期获取日
getDay: function (jsonDate) {
var dateArry = jsonDate.split('-');
var jsonday = parseInt(dateArry[2]);
return jsonday;
},
//输入json日期和日历日期(后面的年),判断json日期是否在两年内
isInYear: function (jsonDate, calenYear) {
var jsonArry = jsonDate.split('-');
var jsonyear = parseInt(jsonArry[0]);
if (jsonyear == calenYear || jsonyear == (calenYear - 1))
return true;
else
return false;
},
//输入json日期和日历日期(年和月),判断json日期是否在日历日期内
isInMonth: function (jsonDate, calendarDate) {
var jsonArry = jsonDate.split('-');
var jsonyear = parseInt(jsonArry[0]);
var jsonmonth = parseInt(jsonArry[1]);
var calenArry = calendarDate.split('-');
var calenyear = parseInt(calenArry[0]);
var calenmonth = parseInt(calenArry[1]);
if (jsonyear == calenyear && jsonmonth == calenmonth)
return true;
else
return false;
},
getNowFormatDate: function () {
var date = new Date();
var seperator = "-";
var year = date.getFullYear();
var month = date.getMonth() + 1;
var strDate = date.getDate();
var currentdate = year + seperator + month + seperator + strDate;
console.log(currentdate);
return currentdate;
},
getDateFromYMD: function (a) {
var arr = a.split("-");
var date = new Date(arr[0], arr[1], arr[2]);
return date;
}
});
调用:
require(['common/myDateClass'], function (myDateClass) {
。。。
。。。
})
调用代码段(任性!!!)
a.js
function myFunctionA(){
console.log('a');
};
b.js
function myFunctionB(){
console.log('b');
};
调用:
require(['js/ab/a','ab/b.js'],function(){
myFunctionA();
myFunctionB();
});
RequireJs调研的更多相关文章
- 前端开发环境搭建 Grunt Bower、Requirejs 、 Angular
现在web开发的趋势是前后端分离.前端采用某些js框架,后端采用某些语言提供restful API,两者以json格式进行数据交互. 如果后端采用node.js,则前后端可以使用同一种语言,共享某些可 ...
- CMS模板应用调研问卷
截止目前,已经有数十家网站与我们合作,进行了MIP化改造,在搜索结果页也能看到"闪电标"的出现.除了改造方面的问题,MIP项目组被问到最多的就是:我用了wordpress,我用了织 ...
- bootstrap + requireJS+ director+ knockout + web API = 一个时髦的单页程序
也许单页程序(Single Page Application)并不是什么时髦的玩意,像Gmail在很早之前就已经在使用这种模式.通常的说法是它通过避免页面刷新大大提高了网站的响应性,像操作桌面应用程序 ...
- 实现一个类 RequireJS 的模块加载器 (二)
2017 新年好 ! 新年第一天对我来说真是悲伤 ,早上兴冲冲地爬起来背着书包跑去实验室,结果今天大家都休息 .回宿舍的时候发现书包湿了,原来盒子装的牛奶盖子松了,泼了一书包,电脑风扇口和USB口都进 ...
- 使用RequireJS并实现一个自己的模块加载器 (一)
RequireJS & SeaJS 在 模块化开发 开发以前,都是直接在页面上引入 script 标签来引用脚本的,当项目变得比较复杂,就会带来很多问题. JS项目中的依赖只有通过引入JS的顺 ...
- 使用gulp解决RequireJS项目前端缓存问题(二)
1.前言 这一节,我们主要解决在上一节<使用gulp解决RequireJSs项目前端缓存问题(一)>末尾提到的几个问题: 对通过require-config.js引入的js文件修改后,没有 ...
- AngularJs2与AMD加载器(dojo requirejs)集成
现在是西太平洋时间凌晨,这个问题我鼓捣了一天,都没时间学英语了,英语太差,相信第二天我也看不懂了,直接看结果就行. 核心原理就是require在AngularJs2编译过程中是关键字,而在浏览器里面运 ...
- angularjs集成requirejs
其实说成使用requirejs加载angularjs应用会更贴切一些 <body> <span ng-controller="homeController"> ...
- 使用gulp解决RequireJS项目前端缓存问题(一)
1.前言 前端缓存一直是个令人头疼的问题,你有可能见过下面博客园首页的资源文件链接: 有没有发现文件名后面有一串不规则的东东,没错,这就是运用缓存机制,我们今天研究的就是这种东西. 先堵为快,猛戳链接 ...
随机推荐
- delay(和setTimeout()的区别
近来几日在写游戏代码时,频繁会用到定时器,偶尔想到有个.delay()方法,用了几次发现两者效果相差很大,遂就仔细考究了一下两者的区别! 1. setTimeout函数是从页面开始的时候计算time的 ...
- 游标cursor
if exists(select * from sys.objects where name='info_one') drop table info_one go create table info_ ...
- javascript keycode大全
keycode 8 = BackSpace BackSpacekeycode 9 = Tab Tabkeycode 12 = Clearkeycode 13 = Enterkeyc ...
- CentOS7 编译安装 Nodejs (实测 笔记 Centos 7.0 + node 0.10.33)
环境: 系统硬件:vmware vsphere (CPU:2*4核,内存2G,双网卡) 系统版本:CentOS-7.0-1406-x86_64-DVD.iso 安装步骤: 1.准备 1.1 显示系统版 ...
- 工作中那些提高你效率的神器(第一篇)_Everything
引言 无论是工作还是科研,我们都希望工作既快又好,然而大多数时候却迷失在繁杂的重复劳动中,久久无法摆脱繁杂的事情. 你是不是曾有这样一种想法:如果我有哆啦A梦的口袋,只要拿出神奇道具就可解当下棘手的问 ...
- jquery修改Switchery复选框的状态
script //选择框 var mySwitch; /* * 初始化Switchery * * classNmae class名 */ function initSwitchery(classNam ...
- shell去除换行和空格2
#!/bin/bash if [ -f str.txt ] ## 如果str.txt存在,则返回true then strval=$(cat str.txt|awk '{printf "%s ...
- centos 格式化分区
#格式化U盘,成fat32 fdisk -l #获取U盘设备信息 #Disk /dev/sdc: 16.0 GB, 16025387008 bytes, 31299584 sectors#Units ...
- gulp + webpack + sass 学习
笔记: new webpack.optimize.CommonsChunkPlugin 核心作用是抽离公共代码,chunks:['index.js','main.js'] 另一个作用就是单独生成一个j ...
- maven filter 乱码,MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence.
<plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactI ...