Titanium,从另一个JS文件调用一个JS文件中的函数

时间:2013-06-24 23:40:48

标签: javascript titanium appcelerator appcelerator-mobile

所以我过去3天一直在尝试每天花费很多时间。我研究了这个死亡但仍然无法得到它。

目标:

-file1.js有一个注意,按下时会调用file2.js中的方法Main_Menu,并打开一个由该方法或函数创建的新窗口。

尝试失败:

-i已经尝试了Ti.include但总是得到一个,无法找到文件错误,我已经尝试将字符串更改为每个可能的路径。

-var file = require(path)但不能使用文件中的方法,例如file.Main_Meue,不起作用

我还尝试了许多其他不想到的事情,但如果有人有任何建议或者您需要更多信息,请询问。请帮助,谢谢你

3 个答案:

答案 0 :(得分:2)

第二个回答

像这样创建第二个窗口:

//file1.js
button.addEventListener('click', function()
{
  var secondWindow = Ti.UI.createWindow({
    url:'file2.js'
  });
  secondWindow.open();
});

file1.js通过file2.js参数使用url创建一个新窗口。在致电file2.js

后,secondWindow.open()现在是您的新窗口

第一个回答

根据本主题的标题,您可以使用fireEvent方法。例如:

file1.js

Ti.App.addEventListener('customEventName', function()
{
  Ti.API.info('function fired from file2.js');
});

file2.js

Ti.App.fireEvent('customEventName');

http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.Proxy-method-fireEvent

答案 1 :(得分:1)

file1.js

var toBeExported ={
a : function(){
    //your code goes here 
  }
};
exports.a = toBeExported.a

file2.js

 var b = require ('file1');
//you can use all the functions that is exported from file1 here.
//you have to create a lib folder or place the file1.js in the existing lib folder for the export/require to work.

希望这会有所帮助。

答案 2 :(得分:0)

这可能是代码结构的问题。基本上你有三种好方法可以根据你使用的版本来实现(实际上你在哪个版本上开始了你的项目:

希望它有所帮助。

相关问题