0

Note: Please don't be intimidated by the word "OSGi."

I am integrating RequireJS on an OSGi-based Virgo server environment (though the details are irrelevant). With an OSGi configuration, I have a root "OSGi bundle" that has JavaScript that needs to set up the main view. Then there are sub-bundles that rely on the root, each with their own JavaScript that needs to be executed.

I have RequireJS running well in the root bundle, with a "main" file that sets up the main view.

Questions: However, I don't know how best to initialize the execution of sub-bundles. Should I add a second <script data-main="main" src="require.js"></script> tag, this time in the sub-bundle to kick off its JavaScript execution? Should I simply import the main JavaScript file as normal in the sub-bundles?

Donald T
  • 10,234
  • 17
  • 63
  • 91
  • Can you add the path when you require the object; subfolder/myobject? – Gusten Jun 26 '13 at 17:46
  • The root path would be "resources/js/main.js" and an example of a particular sub-bundle would be "sub-bundle/resources/js/main.js". – Donald T Jun 26 '13 at 17:51
  • What do you mean by "initialize the execution of sub-bundles"? From your answer below it looks like you mean "initialize the paths configuration"? – explunit Jul 02 '13 at 17:13
  • I'm not intimidated by OSGi. It's the JavaScript bit that scares me! – Neil Bartlett Jul 03 '13 at 19:48
  • @explunit There are two executions that need to be kicked off: the main framework and the sub-bundles (which are consumers of the framework). – Donald T Jul 03 '13 at 19:51

2 Answers2

0

One possibility that I found works, even though it is an ugly and unwanted solution, is to create a global object in the root OSGi bundle that stores the paths relevant to the root. Then sub-bundles can extend their own paths onto this object and reinitialize RequireJS.

Example in root bundle:

var config = {
  paths: {
    // root bundle paths go here . . .
  }
};

require(config.requirejs);

Example in sub-bundle:

$.extend(true, {
  paths: {
    // sub-bundle paths go here . . .
  }
}, config);

require(config.requirejs);

But I don't like having to (seemingly to me) reinitialize RequireJS. Hopefully someone will know of a better solution.

Donald T
  • 10,234
  • 17
  • 63
  • 91
0

I ended up setting up the framework's main JavaScript resources with the following tag in the root bundle:

<script data-main="js/main" src="<c:url value="require.js" />"></script>

. . . with "js/main.js" being:

require(['jquery'], function ($) {
  // main script goes here . . .
})

With the above in the root bundle, sub-bundles simply invoke their own JavaScript with:

<script>require(['sub-bundle/js/script.js'])</script>

So the answer to my question is simply: Use the require function in sub-bundles!

Donald T
  • 10,234
  • 17
  • 63
  • 91