Comments (4)
I decided to look at this again. It seems like it would be a fairly trivial change if you're up for API breakage. Simply adjust Component.prototype.create
to:
Component.prototype.create = function(container) {
debug('create %s', this.id);
var source = container._sources[this._sid];
var loaded = this.loaded;
if (!this.loaded) {
var deps = this.dependencies
, args = {};
for (var i = 0, len = deps.length; i < len; ++i) {
var inst = container.create(deps[i], this);
if (source) {
if (typeof source.fn.scope == 'function') {
inst = source.fn.scope(deps[i], inst, { prefix: source.prefix, options: source.options });
}
}
args[deps[i]] = inst;
}
}
var i = this.instantiate.apply(this, [args]);
if (!loaded && container._expose) {
container._expose.call(container, this.id, i, this.singleton)
}
return i;
}
Note that args
is no longer an array, but an object. As the dependencies are instantiated, they're added to the args
object as the value of a property named after the dependency name.
As I am writing this post, I have realized that dependency names with a path could be a problem. I'm not sure how to handle that scenario.
from electrolyte.
You'd also run into sticky issues with dependencies like "users/db" and "music/db" where there are two objects named the same thing (with different paths).
Honestly, I'm not really a fan of the object idea. It seems like it'll have a lot of edge cases that'll get messy, and if your constructor signature is that big to make this a problem, you probably have a class that is too complicated itself.
from electrolyte.
I agree. I wasn't really trying to solve a constructor with too many dependencies problem. More this pattern:
var foo;
var bar;
// implementation
exports = module.exports = function($foo, $bar) {
foo = $foo;
bar = $bar;
return something;
}
exports['@require'] = [ 'foo', 'bar' ];
The overly long constructor being resolved was just a nice side effect.
However, I don't see how "users/db" and "music/db" would conflict. You'd be passing in an object like:
{
"users/db": {},
"music/db": {}
}
The ugliness there is in accessing those fields.
from electrolyte.
This is an example of how I deal with it:
class Foo {
constructor(args) {
this._settings = args.settings;
this._bar = args.bar
}
}
module.exports['@require'] = [
'config/settings',
'components/bar'
]
module.exports = (settings, bar) => {
return new Foo({
settings: settings,
bar: bar
});
};
from electrolyte.
Related Issues (20)
- Implicit factory resolution HOT 2
- change default as singleton = true HOT 1
- change default as singleton = true HOT 3
- Better error handling on requiring component HOT 2
- Babel transpile with babel-preset-es2015 + async function breaks Electrolyte
- Electrolyte 0.4.0 should reject promise if a dependency is not found HOT 1
- Feature: Override existing Modules
- Release 0.4.0 - IoC.create now returns promise, createAsync doesn't exist HOT 4
- Failure to uphold semver expectations HOT 3
- version 0.3.0 returns objects while 0.4.0 returns promises HOT 11
- Child container
- support for es6 module syntax HOT 5
- Run example in repository : .get() requires callback functions but got a [object Object] Error: .get() requires callback functions but got a [object Object] HOT 5
- Unable to create object "XXXX" required by: unknown HOT 1
- Using it with class HOT 2
- Webpack? HOT 1
- Fix insecure dependencies
- Does IoC.use have to return a function? HOT 1
- Using custom resolvers to substitute components for testing and other purposes
- Is this repo being actively maintained HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from electrolyte.