Skip to content

Commit

Permalink
feat: default optional merging strategy for dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack Ellis committed Feb 3, 2018
1 parent 7f1ec67 commit e14a51f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
6 changes: 5 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## 2.0
## 2.0.0
- Injecting components/mixins/directives is now optional
- There is now a default merging strategy for when using mixins with dependencies

## 1.0.0
- Upgraded to Jpex 2.0.0
- Methods like `clearCache` and `get` now mirror Jpex's `$clearCache` and `$resolve` methods.
- Added decorators
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vue-inject",
"version": "1.0.1",
"version": "2.0.0",
"description": "Dependency Injection for Vue",
"main": "src/index.js",
"scripts": {
Expand Down
14 changes: 13 additions & 1 deletion spec/install.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ test('has $context constant', function (t) {
t.is(vue.self, vue);
});

test('resolves factories as singletons', function (t) {
test('resolves factories as singletons', function (t) {
let {injector, vue} = t.context;

injector.factory('factory', () => ({}));
Expand All @@ -124,3 +124,15 @@ test('has $context constant', function (t) {
t.is(vue2.a, vue.a);
t.is(vue2.b, vue.a);
});

test('sets option merging strategies', function (t) {
let {injector, vue} = t.context;

vue.use(injector);

t.is(typeof vue.config.optionMergeStrategies.dependencies, 'function');

const merged = vue.config.optionMergeStrategies.dependencies(['apple'], { banana: 'b'});

t.deepEqual(merged, [ 'apple', { banana: 'b'}]);
});
5 changes: 4 additions & 1 deletion spec/vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ module.exports = function () {
config[key].call(this);
});
},
$options : {}
$options : {},
config: {
optionMergeStrategies: {},
},
};
return Vue;
};
10 changes: 10 additions & 0 deletions src/install.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ module.exports = function (Vue, options) {
});
}

Vue.config.optionMergeStrategies.dependencies = Vue.config.optionMergeStrategies.depnedencies || function (toVal, fromVal) {
if (!toVal) {
return fromVal;
}
if (!fromVal) {
return toVal;
}
return [].concat(toVal).concat(fromVal).filter((v, i, a) => a.indexOf(v) === i);
};

Vue.mixin({
beforeCreate : function () {
var named = { $context : this };
Expand Down

0 comments on commit e14a51f

Please sign in to comment.