Dojo参考指南: dojo/has

dojo/has提供了标准化的特征检测。 它是基于 has.js 项目(约定了方法签名,比如 has方法,有哪些参数,参数顺序等,相当于提供了一个标准,大家都按这个标准去做的话,那么特征检测在所有的库中就可以通用了)

介绍

源文档:http://dojotoolkit.org/reference-guide/1.9/dojo/has.html

在客户端代码里使用浏览器的user-agent检测 或者特征检测都有各自的缺陷。dojo/has API先添加新的特征测试代码,在需要检测某个特征时,在执行特征测试特码, 并缓存结果, 这句有点不好理解,可以使用代码来解释
has.add("dom",function(){return true}); //特征测试
if(has(dom){  // has(dom) 就是需要先检测某个特征。如果有缓存结果,直接调用缓存,如果没有,执行上面的测试代码
 // do someting
}

dojo/has 模块主要是为dojo的模块提供标准的特征测试和特征检测。 虽然 dojo/has API 实现了 has.js 规范中的方法和 规定的特征名,但dojo模块用的是自己的特征检测。 


dojo/has 特征检测是懒实例化, 什么意思呢? 就是特征测试代码只会在请求某个特征时才会运行,并且为之后相同的特征检测 缓存结果。

Dojo Core 和 Dijit 模块使用 dojo/has 做特征检测。 但还是有很多 DojoX  项目是继续使用dojo.isXXX  的浏览器代理 user agent侦测技术。 这是一个慢慢演进的过程。

dojo/has 可以作为一个插件,通过三元表达式(?:),实现有条件加载模块。


基本的测试是定义在dojo/has模块,在附加在相应模块上的特征和测试,可以增强相应模块的特征检测。 最常使用的 dojo/sniff 就是扩展 dojo/has, 并添加自己额外的测试。

使用staticHasFeatures 在编译配置里, 并结合 Google 的 closure 可以删除无效代码的路径。 查看 builder 文档获得更多的信息.


使用

为了使用这个模块, 它必须被添加到你模块的依赖列表里, 例如:
define(["dojo/has"], function(has){
  if(has("dom")){
    // Do something based on feature
  }
});

特征检测一般会返回一个 "truthy"(Something which evaluates to TRUE, 看了下goole的解释,代表的这个值是需要评估的,这里主要是突出评估,对像,函数,数组都是truthy. 但比如特征检测用到的测试函数,函数就是一个truthy, 但这个测试函数可能会回返一个false的值。这样理解,不知道是对还是错。) 的值。意思是如果功能存在, 返回一个true, 如果不顾在,返回false.

这个模块可以被用于loader的插件, 这么做, 插件的参数是一个三元逻辑表达式, 第一个参数就是feature, 第二个是当 feature为true时要加载的模块,第三个是为false要加载的模块。

require(["dojo/has!feature?package/module:package/other"], function(featureModule){
  // If feature is true, package/module loaded
  // If feature is false, package/other loaded
});

注册一个特征检测需要提供特征检测的名称和测试函数:

has.add("some-test-name", function(global, document, anElement){
  // Feature dection code, returning a truthy value if true
  return true;
});

测试函数应该接受三个参数:

ArgumentDescription
global引用全局作域
document引用document对像
anElement是否需要使用到某个HTML元素 

注意, 任何测试函数都应该自行清理, 这样不会导致内存的溢出。

当你在测试函数后添加一个 true时,会立即运行测试函数。

has.add("some-other-test", function(){
  return false; // Boolean
}, true);

虽然你可以如下,传递一个非封装的函数,但这是不被建议的:

// this is not wrapped in a function, and should be:
has.add("some-other-test", ("foo" in bar)); // or whatever

没有被函数封装, 它会立即执行,而不是之前的那样,在实际需要进行代码检测的路径里执行。


特征名称

 以下的特征测试在Dojo中是有效的。 这个表显示了 哪些模块中有哪些特征测试和测试的名字。
ModuleFeatureDescription
dojo/_base/browserconfig-selectorEnginePre-configured selector engine to use in dojo/query, defaults to dojo/selector/acme.
dojo/_base/connectevents-keypress-typedKeypresses should only occur a printable character is hit
dojo/_base/kernelextend-dojoDefined Dojo modules should push their definitions into the dojo object. In 2.0, it will likely be unusual to augment another object as a result of defining a module. This has feature gives a way to force 2.0 behavior as the code is migrated.
dojo/_base/kerneldojo-guarantee-consoleEnsure that console.log, console.warn, etc. methods are defined
dojo/_base/kerneldojo-debug-messagesLog internal debug messages generated by Dojo, these include deprecated/experimental warnings along with parser auto-required module names.
dojo/_base/kerneldojo-modulePathsConsume module paths defined in config.modulePaths.
dojo/_base/kerneldojo-moduleUrlExpose dojo.moduleUrl method, returns a URL relative to a module. Deprecated in 2.0, should use require.toUrl()
dojo/_base/langbug-for-in-skips-shadowedTest for bug where the for-in iterator skips object properties that exist in Object’s prototype (IE6 - ?).
dojo/_base/loaderdojo-fast-sync-requireAll dojoRequireCallbacks can be released when all non-dojo/require!, dojo/loadInit! modules are either executed, not requested, or arrived. Potential weakness of this algorithm is that dojo/require will not execute callbacks untilall dependency trees are ready.
dojo/_base/loaderconfig-publishRequireResultPublish resolved module values, resulting from a require call, as JavaScript objects referenced by module identifiers in the global namespace.
dojo/_base/windowquirksBrowser is running in Quirks-Mode
dojo/dojohost-node设置Dojo的运行环境在Nodejs平台
dojo/dojohost-rhinoDojo 的运行环境 on the Rhino 平台
dojo/dojodojo-xhr-factory 
dojo/dojodojo-force-activex-xhrForce XHR provider to use ActiveX API (MSXMLHTTP).
dojo/dojonative-xhrBrowser has native XHR API, XMLHttpRequest.
dojo/dojodojo-gettext-apiDojo provides API for retrieving text resource contents from a URL.
dojo/dojodojo-loader-eval-hint-urlModule location should be used as source hint during eval rather than module identifier.
dojo/dojoie-event-behaviorBrowser supports legacy IE event behaviour API (attachEvent versus attachEventListener).
dojo/hashost-browser Dojo 运行在浏览器平台
dojo/hasdom当前平台支持 DOM 
dojo/hasdojo-dom-ready-api当前平台上支持 DOMReady API, 当DOM加载完时,会通知一个监听器,  代码在dojo.js
dojo/hasdojo-sniff 允许扫描dojo.js 角本中的 data-dojo-config 和 djConfig 标签
dojo/hasdom-addeventlistenerStandard DOM event API (addEventListener) supported on the current platform.
dojo/hastouchTouch events are supported on the current platform
dojo/hasdevice-widthAmount of horizontal space in pixels available on the window
dojo/hasdom-attributes-explicitDOM node attributes array only lists explicitly user specified attributes, (W3C standard)
dojo/hasdom-attributes-specified-flagDOM node attribute values provide specified flag to skip attributes user didn’t specify, (IE8)
dojo/hccsshighcontrastBrowser is in ‘high-contrast’ mode
dojo/i18ndojo-preload-i18n-ApiDefine the preload localizations machinery, allow loading of special rollup modules which contain one or more flattened, localized bundles.
dojo/i18ndojo-v1x-i18n-ApiDefine legacy (v1.x) i18n functions
dojo/jsonjson-parsePlatform supports parsing JSON text to JavaScript objects through native API
dojo/jsonjson-stringifyPlatform supports ‘stringify’ method on native JSON API, allowing serialisation of JavaScript objects to JSON text.
dojo/mousedom-quirksBrowser is running in Quirks-Mode
dojo/mouseevents-mouseenterBrowser supports the onmouseenter DOM event
dojo/mouseevents-mousewheelBrowser supports the onmousewheel DOM event
dojo/onjscriptJavaScript environment provided by the JScript platform, dialect of ECMAScript standard that is used in Microsoft’s Internet Explorer.
dojo/onevent-orientationchangeBrowser supports the orientationchange DOM event, used to detect orientation changes in the target device.
dojo/onevent-stopimmediatepropagationBrowser supports the stopImmediatePropagation method on DOM events, used to prevent other event listeners being called.
dojo/queryarray-extensibleNative array implementation supports manual extension (not supported in older versions of IE).
dojo/readydojo-config-addOnLoadConsume addOnLoad configuration property.
dojo/request/handlersactivexBrowser platform has ActiveX API methods, provided by Internet Explorer
dojo/request/scriptscript-readystatechangeDOM supports onreadystatechange event, fired when document.readyState changes
dojo/request/xhrnative-xhrBrowser has native XHR API, XMLHttpRequest
dojo/request/xhrdojo-force-activex-xhrForce XHR provider to use ActiveX API (MSXMLHTTP).
dojo/request/xhrnative-xhr2Browser’s native XHR implementation supports XHR Level 2 API
dojo/request/xhrnative-formdataBrowser has a native FormData implementation, letting user compile set of key/value pairs to send using XMLHttpRequest
dojo/selector/_loaderdom-qsa2.1Browser supports the DOM QuerySelectorAll method available, with Level 2.1 CSS selectors
dojo/selector/_loaderdom-qsa3Browser supports DOM QuerySelectorAll method, with Level 3 CSS selectors
dojo/selector/litedom-matches-selectorBrowser supports the matchesSelector method for testing selector queries directly against DOM nodes.dojo/selector/lite, dom-qsa, Browsers supports the DOM QuerySelectorAll method.”
dojo/sniffairEnvironment is running on the Adobe Air platform
dojo/sniffkhtmlEnvironment is running on the Konqueror-based platform
dojo/sniffwebkitEnvironment is running on the WebKit rendering engine platform
dojo/sniffchromeEnvironment is running on the Chrome browser platform
dojo/sniffsafariEnvironment is running on the Safari browser platform
dojo/sniffmacEnvironment is running on the Mac OS X platform
dojo/sniffquirksBrowser is running in Quirks-Mode
dojo/sniffiosEnvironment is running on the iOS mobile operating system
dojo/sniffandroidEnvironment is running on the Android mobile operating system
dojo/sniffoperaEnvironment is running on the Opera browser platform
dojo/sniffmozillaEnvironment is running on the Mozilla browser platform
dojo/sniffffEnvironment is running on the Firefox browser platform
dojo/sniffieEnvironment is running on the Microsoft Internet Explorer browser platform
dojo/sniffwiiEnvironment is running on the Nintendo Wii browser platform
dijt/BackgroundIframeconfig-bgIframeFlag to create background iframe behind popups like Menus and Dialog. A background iframe prevents problems with popups appearing behind applets/pdf viewers, and also prevents the bleed through select problem on IE6 and IE7.
dijit/_WidgetBasedijit-legacy-requiresMake dijit load modules the application didn’t explicitly require, e.g. dijit/_base/manager, backwards compatibility in non-async mode.
dijit/form/_ExpandingTextAreaMixintextarea-needs-help-shrinkingBrowser platform’s <textarea> element needs manual help to shrink as content changes.
dojox/form/uploader/BaseFormDataBrowser has a native FormData implementation, letting user compile set of key/value pairs to send using XMLHttpRequest
dojox/form/uploader/Basexhr-sendAsBinaryBrowser’s native XHR implementation supports the sendAsBinary method, for sending binary data over XHR.
dojox/form/uploader/Basefile-multipleBrowser supports file input DOM element with multiple file selection attribute, allowing user to select more than one file.
dojox/mobile/Audiomobile-embed-audio-video-supportPlatform supports creating embed tags with audio and video elements.
dojox/mobile/commonmblAndroidWorkaroundTest for Android 2.X transition animation flicker issue
dojox/mobile/commonmblAndroid3WorkaroundTest for Android 3.X transition animation flicker issue
dojox/mobile/scrollabletranslate3dBrowser supports the WebKit-specific CSS transform property, translate3d.
dojox/mobile/sniffbbEnvironment is running on the RIM Blackberry mobile browser platform
dojox/mobile/sniffandroidEnvironment is running on the Android mobile browser platform
dojox/mobile/sniffiphoneEnvironment is running on the iPhone mobile browser platform
dojox/mobile/snifftouchTouch events are supported on the current platform
dojox/mvc/_InlineTemplateMixindom-qsaBrowser supports the DOM QuerySelectorAll method
dojox/mvc/parserExtensiondom-qsaBrowser supports the DOM QuerySelectorAll method
dojox/mvc/parserExtensiondojo-parserBrowser has loaded the dojo/parser module
dojox/mvc/parserExtensiondojo-mobile-parserBrowser has loaded the dojox/mobile/parser module
dojox/mvc/syncmvc-bindings-log-apiEnable debugging messages for MVC module.





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值