cujojs / wire

A light, fast, flexible Javascript IOC container
Other
861 stars 68 forks source link

Using components from child and sibling contexts #175

Open ghost opened 9 years ago

ghost commented 9 years ago

There doesn't seem to be a way to use components in child and sibling contexts from within the spec. Once everything is wired you can of course use them with childContext.someComponent but you can't do that from the spec.

I'd like to propose this:

// child-spec.js
define({
    aComponent: {
        module: "someModule"
    }
});

// parent-spec.js 
define({
    childContext: {
        wire: "./child-spec"
    }
    anotherChildContext: {
        wire: "./another-child-spec"
    }
    aReference: {
        module: { $ref: "childContext.aComponent" }
    }
    // aReference resolves to aComponent in childContext
});

// another-child-spec.js
define({
    anotherReference: {
        module: { $ref: "childContext.aComponent" }
    }
    // anotherReference resolves to aComponent in childContext (a sibling context)
});

Currently you can only use top-level components from ancestor contexts. This includes any child contexts declared in the ancestor contexts; however, you can't directly use any components from them.

A good use for this would be namespacing. For example, if I had a bunch of controllers that I needed to use throughout my application, I could register them under a child context called controller. Then I could just use the components in controller in my spec with "controller.post" or "controller.user" etc. instead of "polluting" the parent namespace with lots of controller names.