Skip to content

Commit

Permalink
feat: strict mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Ellis committed Feb 3, 2018
1 parent e14a51f commit b048826
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,9 @@ let injector2 = injector.spawn();
```
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.

#### strict
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.

### Lifecycle
When registering a factory or service, it's possible to determine the lifecycle.
*As of v0.4, the default lifecycle is set to `class`*.
Expand Down
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 2.0.0
- Injecting components/mixins/directives is now optional
- There is now a default merging strategy for when using mixins with dependencies
- Added a `injector.strict` option which makes all dependencies optional. Note this doesn't affect `injector.get`

## 1.0.0
- Upgraded to Jpex 2.0.0
Expand Down
19 changes: 19 additions & 0 deletions spec/install.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,22 @@ test('sets option merging strategies', function (t) {

t.deepEqual(merged, [ 'apple', { banana: 'b'}]);
});

// strict
test('strict throws when a dep is not found', function (t) {
let {injector, vue} = t.context;

vue.dependencies = 'foofactory';

t.throws(() => vue.use(injector));
t.throws(() => injector.get('foofactory'));
});
test('non-strict does not throw for missing deps', function (t) {
let {injector, vue} = t.context;

vue.dependencies = 'foofactory';
injector.strict = false;

t.notThrows(() => vue.use(injector));
t.throws(() => injector.get('foofactory'));
});
3 changes: 3 additions & 0 deletions src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@ Base.spawn = function (extend) {
}
};

// Allow setting a soft mode
Base.strict = true;

// Install method used by Vue.use
Base.install = install;
15 changes: 13 additions & 2 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ module.exports = function (Vue, options) {
var self = this;

var $typeof = this.$resolve('$typeof');
var strict = this.strict;

function fixName(name) {
if (strict) {
return name;
}
if (name.charAt(0) === '_' && name.substr(name.length -1) === '_') {
return name;
}
return '_' + name + '_';
}

function setProperty(target, name, value) {
Object.defineProperty(target, name, {
Expand All @@ -22,15 +33,15 @@ module.exports = function (Vue, options) {
.forEach(function (dependency) {
switch ($typeof(dependency)){
case 'string': // resolve dependency and attach to the same-named property
setProperty(target, dependency, self.$resolve(dependency, named));
setProperty(target, dependency, self.$resolve(fixName(dependency), named));
break;
case 'object': // resolve each property and use the key as the property name
// Aliases
Object.keys(dependency)
.forEach(function (key) {
var value = dependency[key];
if ($typeof(value) === 'string'){
setProperty(target, key, self.$resolve(value, named));
setProperty(target, key, self.$resolve(fixName(value), named));
}
});
break;
Expand Down

0 comments on commit b048826

Please sign in to comment.