[TIMOB-8997] Android: Add the ability to distribute CommonJS modules along with a native module
GitHub Issue | n/a |
---|---|
Type | New Feature |
Priority | Critical |
Status | Closed |
Resolution | Duplicate |
Resolution Date | 2012-06-14T17:19:14.000+0000 |
Affected Version/s | n/a |
Fix Version/s | n/a |
Components | Android |
Labels | n/a |
Reporter | Marshall Culpepper |
Assignee | Bill Dawson |
Created | 2012-05-04T12:31:01.000+0000 |
Updated | 2017-03-21T22:40:59.000+0000 |
Description
Titanium needs a way to expose a JavaScript wrapper around a native module by providing a way to distribute CommonJS / JavaScript modules along side their native counter parts.
In theory, this might work something like:
@Kroll.module
public class MyModule extends KrollModule
{
@Kroll.method
public void nativeMethod() {}
}
// in assets/myModule.js
var nativeModule = require("my.native.module.id");
exports.wrappedMethod = function() {
return nativeModule.nativeMethod();
}
// now in the app.js
var myModule = require("myModule");
myModule.wrappedMethod();
Dependency on TIMOB-4521 because a) it's in the same sprint; b) it will determine how we handle commonjs, period, in android module projects.
Please follow the linked items (dependencies) as well for status. This is a complex issue because: a) We need to maintain parity, and _none_ of the platforms (Android, iOS, MobileWeb) support this. Therefore parity discussions and test cases will need to occur/be developed. b) This would require that both Javascript-based and native-based module code be executed at runtime, and the way modules are currently architected this won't work. Therefore we need to book substantially more time for this, and I've broken it down to the linked tasks.
At this point, this item basically duplicates TIMOB-9315, so I'm going to close it. Follow the progress of TIMOB-9315, which is already at the pull request stage and therefore should be merged into our master branch in time for 2.1.0. TIMOB-9315 implements the ability to run both javascript (CommonJS) and native code in the same module. The CommonJS file should be assets/[moduleid].js (substituting the actual module id of course.) If we find such a file, we merge its exported members with the native module at runtime. So there's just a few alterations to marshall's suggested code above. It would now look like this:
Note the use of "this" in the my.native.module.id.js file. It provides access to the native methods.
Closing ticket as duplicate.