iOS CommonJS模块返回“undefined is not constructor”错误

时间:2016-06-29 13:30:40

标签: ios module titanium appcelerator commonjs

我正在尝试使用预编译的CommonJS模块创建iOS Titanium模块。正如README文件所说:

  

资产目录中的所有JavaScript文件都是IGNORED,除非您创建了   在这个目录中名为“com.moduletest.js”的文件,在这种情况下它将是   由本机代码包装,编译并用作您的模块。这允许你   运行预编译的纯JavaScript模块。

我已经创建了这样的文件:

function ModuleTest(url){
 if(url){
   return url;
 }
}
exports.ModuleTest = ModuleTest;

我正在使用5.1.2.GA SDK(也尝试使用5.3.0.GA),我可以使用python build.pytitanium build --platform iOS --build-only成功构建模块。 然后,在我的测试应用程序中执行:

var test = require('com.moduletest');
var url = new test.ModuleTest('http://url');

给我这个错误:

undefined is not a constructor

undefined不是构造函数。 我一直在尝试很多替代方案,但似乎没有任何工作,我没有找到有关iOS预编译JS模块的文档的任何帮助。实际上,同样的过程对Android来说非常有用! 你知道为什么吗?

我的环境:

XCode 7.3.1

操作系统   名称 - Mac OS X.   版本 - 10.11.5   架构 - 64位   #CPUs - 8   内存 - 16.0GB

的Node.js   Node.js版本 - 0.12.7   npm版本 - 2.11.3

Appcelerator CLI   安装程序 - 4.2.6   核心包 - 5.3.0

Titanium CLI   CLI版本 - 5.0.9   node-appc Version - 0.2.31

也许这与我的Node版或appc CLI有关,不确定= /

谢谢!

2 个答案:

答案 0 :(得分:3)

有两种解决方案。

1)不要把它放在资产中,而是放在其他人提到的SELECT WeekNum FROM @MonitorTable GROUP BY WeekNum HAVING SUM(CASE WHEN IsProcessed = 1 THEN 0 ELSE 1 END) = 4 文件夹中。

2)将其包装为实际的commonjs模块,如module I wrote

在这两种情况下,您都可以使用/app/lib。在案例2中,您需要将其添加到require('modulename')文件中,就像任何其他模块一样。

您的文件路径将以tiapp.xml或类似名称提供。我的链接模块将向您显示所需的要求和路径。

答案 1 :(得分:0)

我使用了略有不同的模式:

首先是我的"模块"的小片段:

Stopwatch = function(listener) {
    this.totalElapsed = 0; // * elapsed number of ms in total
    this.listener = (listener != undefined ? listener : null); // * function to receive onTick events
};

Stopwatch.prototype.getElapsed = function() {
    return this.totalElapsed;
};

module.exports = Stopwatch;

然后这就是我使用它的方式:

var StopWatch = require('utils/StopWatch');
var stopWatch = new StopWatch(listenerFunction);
console.log('elapsed: ' + stopWatch.getElapsed());