Skip to content

Commit b048826

Browse files
author
Jack Ellis
committed
feat: strict mode
1 parent e14a51f commit b048826

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ let injector2 = injector.spawn();
226226
```
227227
By default this will create a brand new injector, but if you want to share registered services/factories between the two, pass `true` into the function. This will create a new injector that *inherits* the previous one. Any factories registered on the first injector will be available to the second, but not vice versa.
228228

229+
#### strict
230+
If set to `false` then all dependencies will be made optional. If a component's dependency cannot be found, rather than throwing an error it will just be set to `undefined`. Not that this does not affect the `get` function.
231+
229232
### Lifecycle
230233
When registering a factory or service, it's possible to determine the lifecycle.
231234
*As of v0.4, the default lifecycle is set to `class`*.

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 2.0.0
22
- Injecting components/mixins/directives is now optional
33
- There is now a default merging strategy for when using mixins with dependencies
4+
- Added a `injector.strict` option which makes all dependencies optional. Note this doesn't affect `injector.get`
45

56
## 1.0.0
67
- Upgraded to Jpex 2.0.0

spec/install.spec.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,22 @@ test('sets option merging strategies', function (t) {
136136

137137
t.deepEqual(merged, [ 'apple', { banana: 'b'}]);
138138
});
139+
140+
// strict
141+
test('strict throws when a dep is not found', function (t) {
142+
let {injector, vue} = t.context;
143+
144+
vue.dependencies = 'foofactory';
145+
146+
t.throws(() => vue.use(injector));
147+
t.throws(() => injector.get('foofactory'));
148+
});
149+
test('non-strict does not throw for missing deps', function (t) {
150+
let {injector, vue} = t.context;
151+
152+
vue.dependencies = 'foofactory';
153+
injector.strict = false;
154+
155+
t.notThrows(() => vue.use(injector));
156+
t.throws(() => injector.get('foofactory'));
157+
});

src/base.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@ Base.spawn = function (extend) {
3434
}
3535
};
3636

37+
// Allow setting a soft mode
38+
Base.strict = true;
39+
3740
// Install method used by Vue.use
3841
Base.install = install;

src/install.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@ module.exports = function (Vue, options) {
33
var self = this;
44

55
var $typeof = this.$resolve('$typeof');
6+
var strict = this.strict;
7+
8+
function fixName(name) {
9+
if (strict) {
10+
return name;
11+
}
12+
if (name.charAt(0) === '_' && name.substr(name.length -1) === '_') {
13+
return name;
14+
}
15+
return '_' + name + '_';
16+
}
617

718
function setProperty(target, name, value) {
819
Object.defineProperty(target, name, {
@@ -22,15 +33,15 @@ module.exports = function (Vue, options) {
2233
.forEach(function (dependency) {
2334
switch ($typeof(dependency)){
2435
case 'string': // resolve dependency and attach to the same-named property
25-
setProperty(target, dependency, self.$resolve(dependency, named));
36+
setProperty(target, dependency, self.$resolve(fixName(dependency), named));
2637
break;
2738
case 'object': // resolve each property and use the key as the property name
2839
// Aliases
2940
Object.keys(dependency)
3041
.forEach(function (key) {
3142
var value = dependency[key];
3243
if ($typeof(value) === 'string'){
33-
setProperty(target, key, self.$resolve(value, named));
44+
setProperty(target, key, self.$resolve(fixName(value), named));
3445
}
3546
});
3647
break;

0 commit comments

Comments
 (0)