cujojs / wire

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

Extend wire spec with new lifecycle stages/facets #139

Closed webpro closed 10 years ago

webpro commented 10 years ago

I have a controller that wires modules using wire(). Then I can also use wireContext.destroy() to remove them later on.

Now I would like to add lifecycle events, just like the destroy lifecycle/facet mechanism. Namely "detach" and "attach", e.g.:

define('testModule/spec', {
    testModule: {
        create: {
            module: 'testModule/main'
        },
        destroy: 'remove',
        detach: 'onAttach',
        attach: 'onDetach'
    }
});

This way, things are nicely decoupled, and module authors are free to name the methods as they wish (just like for the destroy stage).

What would be the best way to go about this?

I have been looking into the (plugin) docs, but I'm still not sure how to add this facet including the desired behavior (by creating a new plugin). Ideally, it's also possible to use e.g. detach:before as a facet.

webpro commented 10 years ago

As discussed at irc, here's is an out-of-the-box solution:

define('testModule/spec', {
    testModule: {
        create: {
            module: 'testModule/main',
            args: [{ $ref: 'viewNode' }]
        },
        destroy: 'remove'
    },
    detach: { compose: 'testModule.onDetach' },
    attach: { compose: 'testModule.onAttach' }
});

Gonna close this one, if I have more questions I'll open another issue. Thanks @briancavalier!

briancavalier commented 10 years ago

Cool, glad that worked!