You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+82-24
Original file line number
Diff line number
Diff line change
@@ -4,12 +4,22 @@
4
4
5
5
A script loader for AngularJS that perform AMD-like script injection without the need for special syntax.
6
6
7
+
8
+
9
+
10
+
11
+
7
12
# Rationale
8
13
9
14
Say you are like me and love having a Javascript file for every module. And you have a lot of scripts with specific tasks; directives, factories, utility functions. You name it.
10
15
11
16
One solution that we used here at Coders at Work was RequireJS, but I grew tired of it; it is becoming too heavy and doesn't integrate that well with AngularJS.
12
17
18
+
19
+
20
+
21
+
22
+
13
23
# Example
14
24
15
25
So I decided to go ahead and build this little script. Here's how it works:
@@ -25,7 +35,7 @@ So I decided to go ahead and build this little script. Here's how it works:
25
35
>
26
36
> <body>
27
37
> <divng-controller="TestCtrl as test">
28
-
> {{test.value}}<br/>
38
+
> {{test.value}}
29
39
> </div>
30
40
> </body>
31
41
> </html>
@@ -47,9 +57,14 @@ So I decided to go ahead and build this little script. Here's how it works:
47
57
> });
48
58
> ```
49
59
50
-
Angularjs-loader automatically sees that angular.module() has a dependency to 'test_ctrl', and as such loads the script automatically when it's ready. When all `angular.module()` calls are done, it automatically bootstrap angular.
60
+
Angularjs-loader automatically sees that angular.module() has a dependency to 'test_ctrl', and loads the script automatically when it's ready. When all `angular.module()` calls are done, it automatically bootstrap angular.
61
+
62
+
As long as you stick to AngularJS, there's no need for shims, paths or configurations. A blocking module makes it simple to include other libraries as well.
63
+
64
+
65
+
66
+
51
67
52
-
As long as you stick to AngularJS, there's no need for shims, paths or configurations.
53
68
54
69
# API
55
70
@@ -65,6 +80,8 @@ It takes 3 arguments:
65
80
* `onbootstrap`. Code to evaluate when the Angular is finished loading.
66
81
* `noinit`. Any non-empty string to prevent the AngularJS-Loader from automatically starting the initialization process. See [Tests](#Tests).
67
82
83
+
84
+
68
85
## Angular Modules
69
86
70
87
The loader overrides `angular.module()` and does two things:
@@ -75,11 +92,13 @@ The loader overrides `angular.module()` and does two things:
75
92
76
93
As you use `angular.module()` to create a module, it will load its dependencies recursively. Using `angular.module()` to only access the module itself is okay and has no side effect.
77
94
95
+
96
+
78
97
## Custom Scripts
79
98
80
99
When you need to load custom scripts you should use the new function `angular.loader()`. It takes 2 arguments and returns a simplified promise object.
81
100
82
-
The first argument is either a filename or an array of filename to load. Remember that these will be transformed using [path transform](#Configuration).
101
+
The first argument is either a filename or an array of filename to load. Remember that these will be transformed using the [path transform](#Configuration).
83
102
84
103
The second argument is an object of named options:
85
104
@@ -93,12 +112,16 @@ Omitting this option performs no checks at all and unlock the bootstrap when the
93
112
94
113
The object returned can be used as a simpler Angular deferred promise object, but there's no `finally` method on it, only `catch` and `then`. The promise will be fulfilled after the checker has returned `true` for all the scripts.
95
114
115
+
116
+
96
117
## Locking the Bootstrap
97
118
98
119
You can lock bootstrapping of AngularJS with `angular.loader.lock()` (and unlock it with `angular.loader.unlock()`). The functions take a name as parameter. Locking the same name twice, unlocking an unlocked or unknown name are all errors. The only limit on locks are that you cannot lock/unlock once the bootstrap happened. Anything after bootstrapping angular is considered an error.
99
120
100
121
Locking and unlocking before the end of loading all the modules and their dependencies will simply result in waiting for the dependencies to finish loading.
101
122
123
+
124
+
102
125
## <aname="Configuration"></a> Configuration
103
126
104
127
Configuration is set by calling `angular.loader.config()`. The function takes an object as argument and update the internal configuration.
@@ -108,48 +131,83 @@ The list of configuration options is as follow:
108
131
* `path`. A map of name to full path (or `null`). If a module name is present in this map, the loader will use this path instead of the name to load. If the full path in the map is `null`, the script will return
109
132
* `pathTransform`. A list of functions that take a path and return a path or `null`. This is to allow transforming a module or script name to its file path. The transform chain will not be executed if the path is part of the `path` option above or is a full URI.
110
133
* `checker`. A checker map for modules. See loading custom scripts above. This is to simplify the calls to `angular.loader()`.
134
+
* `error`. An error handler. If specified, NO error will be thrown or outputted, they will all be passed to the handler.
111
135
112
136
The process of getting a script path from a module name is as follow:
113
137
114
138
```javascript
115
-
var name; // Original name for the module.
116
-
var path = name in config.path ? config.path[name] : name; // Path to load.
117
-
if (path === null) return;
118
-
if (isUri(path)) return path;
119
-
120
-
for (var i = 0; i < config.transformPath.length; i++) {
121
-
old_path = path;
122
-
path = config.transformPath[i](path, name);
123
-
if (path === null) {
124
-
path = old_path;
125
-
break;
126
-
}
127
-
}
128
-
129
-
if (isUri(path)) return path;
130
-
if (path[0] != '/') path = '/' + path;
131
-
if (!/\.js$/.test(path)) path += '.js';
132
-
return path;
139
+
var name; // Original name for the module.
140
+
var path = name in config.path ? config.path[name] : name; // Path to load.
141
+
if (path === null) return;
142
+
if (isUri(path)) return path;
143
+
144
+
for (var i = 0; i < config.transformPath.length; i++) {
145
+
old_path = path;
146
+
path = config.transformPath[i](path, name);
147
+
if (path === null) {
148
+
path = old_path;
149
+
break;
150
+
}
151
+
}
152
+
153
+
if (isUri(path)) return path;
154
+
if (path[0] != '/') path = '/' + path;
155
+
if (!/\.js$/.test(path)) path += '.js';
156
+
return path;
133
157
```
134
158
159
+
160
+
135
161
## <aname="Tests"></a> Tests (Karma and Jasmine)
136
162
137
-
*__To Be Done!__*
163
+
Some Karma tests are included to cover the most basis cases. Tests are added regularly for better coverage.
164
+
165
+
To run the tests, simply run `karma run` in the angularjs-loader folder.
166
+
138
167
139
-
-----
140
168
141
169
# ToDo
142
170
171
+
For version 1.0:
172
+
143
173
1. Minification pre-step. Having a script that takes all `angular.module()` calls and merge the scripts into a single file (or multiple).
144
174
175
+
Future:
176
+
177
+
1. Nothing really.
178
+
179
+
180
+
181
+
182
+
183
+
145
184
# Suggestions / Questions / Praises
146
185
147
186
If you think of something, create an issue!
148
187
188
+
189
+
190
+
191
+
192
+
149
193
# FAQ
150
194
151
195
152
196
197
+
198
+
### Are there any complex examples out there?
199
+
200
+
None for now. I want to add some cookbook for this as it can be quite powerful but hard to grasp. I figure that for now the simple case will be enough for most uses.
201
+
202
+
### Requirejs does X. Why don't you do it?
203
+
204
+
A mixture of reasons, really. RequireJS has a couple of devs behind it and more bandwidth. Also, I wanted to keep the size really low (AJS-Loader is currently under 3650 bytes minified and uncompressed, while RequireJS sits at 15kb) and performance really high (benchmarks coming later).
205
+
206
+
Some stuff had to be sacrificed. Mainly, there's no plugin for AJS-Loader, you can't load things that are not scripts (but can load stuff outside of AngularJS modules, of course), it is mainly meant as a developer only script (meaning that production code should use the `concat` script I'm working on), and while path transforms and checkers can be quite powerful, they're also more complex than other solutions.
0 commit comments