1

We're building web app that uses RequireJS for modular development/dependency management.
We kick things of with the following:

<script data-main="js/main" src="js/libs/require/require.js"></script>

The web app also uses a third-party applet that is required for communication with a mainframe system. The applet has a callback function that executes once the api for communicating with it has been initialized, with the api object passed in as a parameter. e.g.

/*
 * This function is invoked automatically after the Reflection session has 
 * completed its initialization. A reference to the JavaScript API is passed
 * in as a parameter.
 */
 function jsapiInitialized( api ) {

    return api;

 }  

Basically, the above needs to be a dependency for many of the modules within the app.

How can I make the above function a module dependency, so the api object is available to those modules requiring it?

1 Answer 1

3

It seems that you want to use asynchronous like define method for your API. To do it with require.js you'll need to create a small plugin. As result you'll have the following files:

1) myapi.js (API simulation file)

// this is actually simulation of your third party code
setTimeout(function() {
    if(typeof jsapiInitialized === 'function') jsapiInitialized('hello!');
}, 1000);

2) myplugin.js (plugin which handles your API callback)

define(function(){
    return {
        load : function(name, req, onLoad, config){
            window.jsapiInitialized = function(api) {
                onLoad(api);
            };
            require([name]);
        }
    };
});

3) main.js (to test your plugin)

require(['myplugin!myapi'], function(api){
    alert(api);
});

(where "myapi" can be a path to your real API script)

Sign up to request clarification or add additional context in comments.

14 Comments

We don't have access to any api script (that I'm aware of), we simply add the jsapiInitialized() to the page and the applet calls it, passing in the api parameter. Is that a problem?
That's no problem for you. You don't need to modify anything. As you can see jsapiInitialized function is created automatically by this piece of code: window.jsapiInitialized = function(api) {.... And when it's get called from third party script this function simply pass API via requirejs internals.
OK, since jsapiInitialized is called automatically, is myapi.js needed? If not, what would require() parameters look like in main.js?
No, you don't need myapi.js - it is just simulation of your case. This is actually your "third party script". I've make these local files to test my answer. Your require parameters should look like: require(['myplugin!http://example.com/api.js']) (you can use relative path also)
Ok, that is what I thought but wasn't sure. I just need to find the location of the api.js. That should be easy... Thank you again for your help! BTW, is this typically what plugins are used for, outside dependencies?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.