requirejs源码分析: requirejs 方法–2. context.require(deps, callback, errback);
上一篇 requirejs源码分析: requirejs 方法–1. 主入口 中的return context.require(deps, callback, errback); 调用的是makeRequire方法中的localRequire
function localRequire(deps, callback, errback) {
var id, map, requireMod;if (options.enableBuildCallback && callback && isFunction(callback)) {
callback.__requireJsBuild = true;
}if (typeof deps === 'string') {
if (isFunction(callback)) {
//Invalid call
return onError(makeError('requireargs', 'Invalid require call'), errback);
}//If require|exports|module are requested, get the
//value for them from the special handlers. Caveat:
//this only works while module is being defined.
if (relMap && hasProp(handlers, deps)) {
return handlers[deps](registry[relMap.id]);
}//Synchronous access to one module. If require.get is
//available (as in the Node adapter), prefer that.
if (req.get) {
return req.get(context, deps, relMap, localRequire);
}//Normalize module name, if it contains . or ..
map = makeModuleMap(deps, relMap, false, true);
id = map.id;if (!hasProp(defined, id)) {
return onError(makeError('notloaded', 'Module name "' +
id +
'" has not been loaded yet for context: ' +
contextName +
(relMap ? '' : '. Use require([])')));
}
return defined[id];
}//Grab defines waiting in the global queue.
intakeDefines();//Mark all the dependencies as needing to be loaded.
context.nextTick(function () {
//Some defines could have been added since the
//require call, collect them.
intakeDefines();//实例化Module对象
requireMod = getModule(makeModuleMap(null, relMap));//Store if map config should be applied to this require
//call for dependencies.
requireMod.skipMap = options.skipMap;//初始化 Module 对象
requireMod.init(deps, callback, errback, {
enabled: true
});checkLoaded();
});return localRequire;
}
这里主要是方法:
context.nextTick(function () {
//Some defines could have been added since the
//require call, collect them.
intakeDefines();//实例化Module对象
requireMod = getModule(makeModuleMap(null, relMap));//Store if map config should be applied to this require
//call for dependencies.
requireMod.skipMap = options.skipMap;//初始化 Module 对象
requireMod.init(deps, callback, errback, {
enabled: true
});checkLoaded();
});
这里使用context.nextTick是延迟4ms再执行(异步)。 这里不能很理解作者的目的。 是为了不卡主线程?
该方法主要流程:
1. 实例化Module。
requireMod = getModule(makeModuleMap(null, relMap));
Module也是上下文newContext里的对象。这个是核心模块。包含以下主要的属性。
this.events = getOwn(undefEvents, map.id) || {};
this.map = map;
this.shim = getOwn(config.shim, map.id);
this.depExports = [];
this.depMaps = [];
this.depMatched = [];
this.pluginMaps = {};
this.depCount = 0;
该对象是封装模块,存在模块信息(shim,依赖等。)。 还包含对模块的加载,依赖加载,通知依赖等。
requirejs源码分析: requirejs 方法–2. context.require(deps, callback, errback);的更多相关文章
- requirejs源码分析: requirejs 方法–1. 主入口
该方法是 主要的入口点 也是最常用的方法. req = requirejs = function (deps, callback, errback, optional) { //Find the ri ...
- requirejs源码分析: define 方法
define = function (name, deps, callback) { var node, context; //Allow for anonymous modules ...
- requirejs源码分析: 路径
1. 没有设置baseUrl(一般我们都会设置baseurl) 在没有设置baseUrl时, 默认 baseurl: "./" 当指定data-mai ...
- requirejs源码分析: config中shim
shim处理的源码: //Merge shim if (cfg.shim) { eachProp(cfg.shim, funct ...
- requirejs源码分析,使用注意要点
本文将深度剖析require.js代码,为了是大家更高效.正确的去使用它,本文不会介绍require的基本使用! 概要 先来一个流程图来概要一下大概流程 在require中,根据AMD(Asynchr ...
- mybatis源码分析(方法调用过程)
十一月月底,宿舍楼失火啦,搞得20多天没有网,目测直到放假也不会来了... 正题 嗯~,其实阅读源码不是为了应付面试,更重要的让你知道,大师是怎样去写代码的,同样是用Java,为啥Clinton Be ...
- requirejs源码分析
- WorkerMan源码分析(resetStd方法,PHP中STDIN, STDOUT, STDERR的重定向)
WorkerMan中work.php中 resetStd 方法中代码如下 public static function resetStd() { if (!static::$daemonize || ...
- 【requireJS源码学习01】了解整个requireJS的结构
前言 现在工作中基本离不开requireJS这种模块管理工具了,之前一直在用,但是对其原理不甚熟悉,整两天我们来试着学习其源码,而后在探寻其背后的AMD思想吧 于是今天的目标是熟悉requireJS整 ...
随机推荐
- hdu2049 不容易系列之(4)——考新郎 错排+组合 一共有N对新婚夫妇,N个新娘随机坐成一排,每个新郎只能选一个, 其中有M个新郎找错了新娘,求发生这种情况一共有多少种可能.
不容易系列之(4)——考新郎 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- php 循环简写
<?php class a{ public function news($news){ $articles = array(); foreach ($news as $key => $va ...
- 第一百九十三节,jQuery EasyUI,Draggable(拖动)组件
Draggable(拖动)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 Draggable(拖动)组件的使用方法,这个组件不依赖于其 他组 ...
- Spring MVC资源绑定视图解析器
ResourceBundleViewResolver使用属性文件中定义的视图bean来解析视图名称. 以下示例显示如何使用Spring Web MVC框架中的ResourceBundleViewRes ...
- struts.properties文件
此配置文件提供了一种机制来更改默认行为的框架.其实所有的struts.propertiesconfiguration文件中包含的属性也可以被配置在web.xml中使用的init-param,以及在st ...
- python3----冒泡排序
array = [1, 6, 7, 2, 9, 4] for i in range(len(array)-1, 1, -1): for j in range(0, i): if array[j] &g ...
- python3----练习题(三级菜单)
三级菜单程序. 运行程序输出第一级菜单. 选择一级菜单某项,输出二级菜单,同理输出三级菜单. 退出时返回上一级菜单 menu = { '北京市': { '东城区': { 'aa', 'bb', }, ...
- lucene中的IndexWriter.setMaxFieldLength()
lucene中的IndexWriter.setMaxFieldLength() 老版本的Lucene中,IndexWriter的maxFieldLength是指一个索引中的最大的Field个数. 这个 ...
- Android WebView-应用内嵌入浏览器
移动应用开发,web app.Native app的讨论已经很久了,纯粹的web app还很少,多少能见到Native + web混合的app,混合的app是在Native app中写一个浏览器加载 ...
- 2318: Spoj4060 game with probability Problem
2318: Spoj4060 game with probability Problem Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 356 Sol ...