b2科目四模拟试题多少题驾考考爆了怎么补救
b2科目四模拟试题多少题 驾考考爆了怎么补救

RequireJS学习笔记(2)

电脑杂谈  发布时间:2016-04-19 09:55:41  来源:网络整理

//A module definition inside foo/title.js. It uses //my/cart and my/inventory modules from before, //but since foo/bar.js is in a different directory than //the "my" modules, it uses the "my" in the module dependency //name to find them. The "my" part of the name can be mapped //to any directory, but by default, it is assumed to be a //sibling to the "foo" directory. define(["my/cart", "my/inventory"], function(cart, inventory) { (title) { return title ? (window.title = title) : inventory.storeName + ' ' + cart.name; } } );

模块也可以不必返回对象,任何有效的返回都是可以的,比如上面的例子,返回一个函数作为其模块定义。

带名字的模块

//Explicitly defines the "foo/title" module: define("foo/title", ["my/cart", "my/inventory"], function(cart, inventory) { //Define foo/title object in here. } );

(???)这里明确的定义了foo/title模块,这里其实是一种优化手段,我们这里可以明确的命名模块,但是若是文件目录变化我们就要更改名称了。

其他说明

一个js文件应该只定义一个模块,并与文件目录一致,因为最后有个神马优化工具可以优化。。,。

在定义模块时将各个模块关联起来,

相对模块生产url,你可能需要使用一个相对模块的url:

define(["require"], function(require) { var cssUrl = require.toUrl("./style.css"); });

PS:尼玛,我今天才知道我英语有多差。。。

循环依赖

我们有时候会定于循环依赖的模块,比如a需要b并且b需要a,在这个情况下当b模块调用时他将会从a获得一个undefined值,b可以通过require方法取得一个比较晚的模块。

//Inside b.js: define(["require", "a"], function(require, a) { (title) { return require("a").doSomething(); } } );

你通常不需要使用require方法获取一个模块,在循环依赖时候需要用到(读不懂了。。。)

exports

//Inside b.js: define(function(require, exports, module) { a = require("a"); exports.foo = function () { return a.bar(); }; });

//Inside b.js: define(['a', 'exports'], function(a, exports) { exports.foo = function () { return a.bar(); }; });

配置选项

我们可以通过以下方法,对require进行配置:

require.config({ baseUrl: , paths: { }, waitSeconds: 15 }); require( [], function(someModule, myModule) { } );

baseUrl

模块查找的根目录,默认情况与data-main所赋值处于同一目录

paths

该项用于配置那些不在baseUrl下的模块,这个指定的path假定是baseUrl的相对路径,若是以/开头的话就不是了。

这里的id会自动加上.js,我们要获取一个路径时,一般这个样子干:

require.toUrl()

shim

传统浏览器的全局脚本不使用define去声明依赖关系和模块设置值的依赖。

Ps:读不懂,还是看代码算了:

requirejs.config({ shim: { 'backbone': { deps: ['underscore', 'jquery'], exports: 'Backbone' }, 'underscore': { exports: '_' }, 'foo': { deps: ['bar'], exports: 'Foo', init: function (bar) { return this.Foo.noConflict(); } } } }); define(['backbone'], function (Backbone) { return Backbone.Model.extend({}); });

这个例子假定backbone等依赖库已经在baseUrl中,若是没有就需要配置。

requirejs.config({ shim: { 'jquery.colorize': { deps: ['jquery'], exports: 'jQuery.fn.colorize' }, 'jquery.scroll': { deps: ['jquery'], exports: 'jQuery.fn.scroll' }, 'backbone.layoutmanager': { deps: ['backbone'] exports: 'Backbone.LayoutManager' } } });

结语

读不下去啦,暂时这样吧。。。。。。

以上就是关于requirejs的全部内容,相信你一定会非常满意。


本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/shenmilingyu/article-1777-2.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...