Reputation: 10667
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?
Upvotes: 0
Views: 259
Reputation: 10667
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!
Upvotes: 0
Reputation: 10667
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.
Upvotes: 0