Git Product home page Git Product logo

Comments (4)

jsumners avatar jsumners commented on July 26, 2024

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.

jaredhanson avatar jaredhanson commented on July 26, 2024

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.

jsumners avatar jsumners commented on July 26, 2024

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.

fwoelffel avatar fwoelffel commented on July 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.