|
| 1 | +--- |
| 2 | +layout: default |
| 3 | +title: requirejs-dplugins/has |
| 4 | +--- |
| 5 | + |
| 6 | +# requirejs-dplugins/has |
| 7 | + |
| 8 | +`requirejs-dplugins/has` provides standardized feature detection with an extensible API. It also implements the |
| 9 | +[requirejs plugin api](http://requirejs.org/docs/plugins.html) to provide conditional module loading. |
| 10 | + |
| 11 | +It is based on the conventions in the [has.js project](https://github.com/phiggins42/has.js). |
| 12 | + |
| 13 | +Note: This plugin does NOT include any feature tests. |
| 14 | + |
| 15 | +##### Table of Contents |
| 16 | +[Features](#features) |
| 17 | +[Adding tests](#adding-tests) |
| 18 | +[Use as module](#use-as-module) |
| 19 | +[Use as plugin](#use-as-plugin) |
| 20 | + |
| 21 | +## Features |
| 22 | + |
| 23 | +* __lazy evaluation__: Tests are only evaluated when they are required by the application. |
| 24 | +* __test result caching__: tests results are stored for later use to minimize the impact on resources. |
| 25 | +* __static configuration__: Tests can be set statically using requirejs configuration, providing support for configuration |
| 26 | +flag. |
| 27 | +* __requirejs plugin__: Use ternary operator to conditionally load modules. |
| 28 | + |
| 29 | +## Adding tests |
| 30 | + |
| 31 | +Since the plugin itself does not include any tests, the first thing to do is add one. |
| 32 | + |
| 33 | +### Using has.add() |
| 34 | + |
| 35 | +The plugin provides an `add` function to add new tests, with the following signature: |
| 36 | + |
| 37 | +```js |
| 38 | +has.add(name, test, now, force) |
| 39 | +``` |
| 40 | +1. name _(String)_: The test name. |
| 41 | +1. test _(Boolean or Function)_: The test function or directly a `Boolean` value. The test function will be call with a |
| 42 | +single `global` argument pointing to the global scope. |
| 43 | +1. now _(Boolean)_: If true, evaluate the test function now instead of when it will be needed. |
| 44 | +1. force _(Boolean)_: If true, add the test even if a test with the same name already exists. |
| 45 | + |
| 46 | +#### Sample |
| 47 | +```js |
| 48 | +define(["requirejs-dplugins/has"], function (has) { |
| 49 | + // using a boolean directly |
| 50 | + has.add("host-browser", typeof window !== "undefined"); |
| 51 | + |
| 52 | + // using a test function and overwriting previous tests |
| 53 | + has.add("host-browser", function (global) { |
| 54 | + return global.window !== undefined; |
| 55 | + }, false, true); |
| 56 | +}); |
| 57 | +``` |
| 58 | + |
| 59 | +### Using static configuration |
| 60 | + |
| 61 | +The plugin will look for static configuration in a hashmap provided through |
| 62 | +[requirejs module configuration](http://requirejs.org/docs/api.html#config-moduleconfig). |
| 63 | + |
| 64 | +#### Sample |
| 65 | +```js |
| 66 | +require.config({ |
| 67 | + config: { |
| 68 | + "requirejs-dplugins/has": { |
| 69 | + // Create a bidi flag. |
| 70 | + bidi: true |
| 71 | + } |
| 72 | + } |
| 73 | +}); |
| 74 | +``` |
| 75 | + |
| 76 | +## Use as module |
| 77 | +To access the tests, the plugin exports a main function taking a test name as argument and returning the test result. |
| 78 | + |
| 79 | +#### Sample |
| 80 | +```js |
| 81 | +define(["requirejs-dplugins/has"], function (has) { |
| 82 | + if (has("bidi")) { |
| 83 | + // Do something special to support bidi |
| 84 | + } |
| 85 | +}); |
| 86 | +``` |
| 87 | + |
| 88 | +## Use as plugin |
| 89 | +Ternary operator can be used to conditionally load modules. It uses the following syntax: |
| 90 | + |
| 91 | +``` |
| 92 | +test?moduleA:moduleB |
| 93 | +``` |
| 94 | + |
| 95 | +If `test` return true, `moduleA` will be loaded, otherwise it will be `moduleB`. |
| 96 | +Note that `moduleA` and `moduleB` are optional and if the expression doesn't resolve to a module, the plugin returns |
| 97 | +`undefined`. |
| 98 | + |
| 99 | +Ternary operation can be chained to run another test if the first one fails. |
| 100 | +``` |
| 101 | +test1?moduleA:test2?moduleB:moduleC |
| 102 | +``` |
| 103 | +If `test1` is true, the plugin loads `moduleA`. |
| 104 | +If `test1` is false but `test2` is true, the plugin loads `moduleB`. |
| 105 | +If `test1` is false and `test2` is false, the plugin loads `moduleC`. |
| 106 | + |
| 107 | +#### Sample |
| 108 | +```js |
| 109 | +define(["requirejs-dplugins/has!bidi?./bidiWidget:./Widget"], function (widget) { |
| 110 | + // Do something with the widget |
| 111 | +}); |
| 112 | +``` |
0 commit comments