Search code examples
wirejs

'import' a cujojs/wire context into another


I'm looking for a way to realize the following use-case:

  1. I have many modules and each one of them has a wire spec that exposes its components
  2. To assemble an application, I select the modules and use their wire-spec
  3. The wire-spec of the application is the merge of wire-specs of used modules: (3.1) I start by 'requiring' the wire-spec of each module as objects. (3.2) Then, I merge the objects. (3.3) And, finally, I return the result as the object defining the wire-spec of the application.

Here is a sample of an application context-spec:

define(["jquery", "module1-wire-spec", "module2-wire-spec"], function(jquery, module1WireSpec, module2WireSpec) {
    return jquery.extend(true, module1WireSpec, module2WireSpec);
});

I have read several times wire documentation hoping to find a 'native' way to do the above but I failed so far to find one.

A 'native' way would be a factory like the 'wire' factory but instead of creating a child-context for each module, I'm looking to see the components of each module as direct components of the application context.

Spring, for instance, allows importing a context definition into another one and the result is as if the content of the imported context has been inlined with the importing context.


Solution

  • A new feature has been added to cujojs/wire to allow import of contexts.

    As of version 0.10.8, the keyword imports accepts:

    • a string for a single context import,
    • or an array for a list of contexts import.

    Check here for more details.