Skip to content

Commit 83a8cac

Browse files
committed
add has documentation and update README
1 parent 3064c03 commit 83a8cac

File tree

2 files changed

+118
-38
lines changed

2 files changed

+118
-38
lines changed

README.md

Lines changed: 6 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
Set of AMD plugins for RequireJS. It contains the following plugins:
44

5+
* [has](#has)
56
* [i18n](#i18n)
67
* [maybe](#maybe)
78

89
## Status
910

1011
No official release yet.
11-
12+
1213
## Migration
1314

1415
## Licensing
@@ -38,46 +39,13 @@ Then install dependencies with bower (or manually from github if you prefer to):
3839
$ bower install
3940

4041
## Documentation
41-
### i18n
42-
This plugin is based on requirejs i18n but modified to support nls built layer.
43-
44-
#### When the application IS NOT optimized
45-
If an application is not built, this plugin will behave exactly like the i18n plugin from RequireJS.
46-
The appropriate documentation can be found on [RequireJS website](http://requirejs.org/docs/api.html#i18n).
47-
48-
#### When the application IS optimized
49-
Once the application is built, the plugin offer three new options:
50-
51-
```
52-
requirejs.config({
53-
config: {
54-
i18n: {
55-
layerOnly: bool, //default: false
56-
languagePack: bool, //default: false
57-
enhanceLayer: bool, //default: true
58-
}
59-
}
60-
61-
});
62-
```
63-
* `layerOnly` (default: false)
64-
65-
If `layerOnly = true`, the plugin is only looking for nls bundles in built nls layers. `layerOnly` must be true if only the built nls layers are deployed with the application.
66-
If the individual nls bundles are also deployed, `layerOnly` should be false.
6742

68-
* `languagePack` (default: false)
69-
70-
If true, this option gives the possibility to add new locales to the application just by adding the corresponding built nls layer. However, this will result in more http requests (and some expected 404 in the console), so it should only be used in low latency environments (like cordova applications).
71-
* `enhanceLayer` (default: true)
43+
### has
44+
See [docs/has.md](./docs/has.md).
7245

73-
This option is ignored if `layerOnly = true`.
46+
### i18n
47+
See [docs/i18n.md](./docs/has.md).
7448

75-
This option defines the behaviour of the plugin when a bundle is retrieved from a layer with a less specific locale than requested (ie: bundle `en` from layer when `en-us` was requested).
76-
77-
* If `enhanceLayer = false`, the plugin only uses the bundle from the layer. This is useful when a part of the application is an already built package distributed without the individual nls bundles.
78-
79-
* If `enhanceLayer = true`, the plugin will try to load a more specific individual bundle if one exist.
80-
8149

8250

8351
### maybe

docs/has.md

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

Comments
 (0)