All communication between JavaScript and the game goes through the engine
module.
There are two ways for invoking native code from JavaScript and vice-versa.
Events allow to call multiple handlers in both directions, but they can not return any value. To register a JavaScript handler for an event use the engine.on
method. Detailed documentation for the events is in the Binding documentation.
Promises are used to return results from C++ to JavaScript. Coherent UI promises are modeled after the Promises/A specification. For samples how to use the promise objects returned by engine.call
see JavaScript triggering C++.
Coherent UI has an implementation of promises, but it is possible to use any implementation that has deferred objects and they have resolve
and reject
methods. This allows you to use a promises library of your choice or one that better integrates with the rest of your code.
To change the promises implementation you have to define engineCreateDeferred
function prior to including coherent.js script in your HTML. That function should return a new deferred object each time it is called.
To use JQuery Deferred your HTML should look like this:
Or to use the when.js library: