Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-8997] Android: Add the ability to distribute CommonJS modules along with a native module

GitHub Issuen/a
TypeNew Feature
PriorityCritical
StatusClosed
ResolutionDuplicate
Resolution Date2012-06-14T17:19:14.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsAndroid
Labelsn/a
ReporterMarshall Culpepper
AssigneeBill Dawson
Created2012-05-04T12:31:01.000+0000
Updated2017-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();

Comments

  1. Bill Dawson 2012-05-10

    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.
  2. Bill Dawson 2012-05-30

    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.
  3. Bill Dawson 2012-06-14

    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:
       @Kroll.module
       public class MyModule extends KrollModule
       {
           @Kroll.method
           public void nativeMethod() {}
       }
       
       // in assets/my.native.module.id.js
       
       exports.wrappedMethod = function() {
          return this.nativeMethod();
       }
       
       // now in the app.js
       
       var myModule = require("my.native.module.id");
       myModule.wrappedMethod();
       
    Note the use of "this" in the my.native.module.id.js file. It provides access to the native methods.
  4. Lee Morris 2017-03-21

    Closing ticket as duplicate.

JSON Source