简体   繁体   English

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

[英]Titanium, calling a function in one JS file from another JS file

so i have been trying for many hours a day now for the past 3 days.i researched this to death but still cannot get it. 所以我在过去的3天里一天都在尝试了几个小时。我研究了这个到死,但仍然无法得到它。

Goal: 目标:

-file1.js has a buttion that when pressed will call method Main_Menu in file2.js and will open a new window created by that method, or function. -file1.js有一个按钮,按下时会调用file2.js中的方法Main_Menu,并打开由该方法或函数创建的新窗口。

failed attempts: 尝试失败:

-i have tried Ti.include but always getting a, cant find file error, i have tried changing string to every possible path. -i已经尝试了Ti.include但总是得到一个,无法找到文件错误,我已经尝试将字符串更改为每个可能的路径。

-var file = require(path) but can not use the method inside the file, for example file.Main_Meue, does not work -var file = require(path)但不能使用文件内的方法,例如file.Main_Meue,不起作用

I have also tried many other things that do not come to mind but if anyone has any advice or you need more information just ask. 我还尝试了许多其他不想到的事情,但如果有人有任何建议或者您需要更多信息,请询问。 PLEASE HELP, AND THANKYOU 请帮助,谢谢

second answer 第二个答案

Create the second window like so: 像这样创建第二个窗口:

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

file1.js creates a new window using file2.js via the url parameter. file1.js通过url参数使用file2.js创建一个新窗口。 file2.js is now your new window after calling secondWindow.open() 调用secondWindow.open()后, file2.js现在是你的新窗口

First answer 第一个答案

Based off the title of this topic, you can use the fireEvent method. 基于本主题的标题,您可以使用fireEvent方法。 For example: 例如:

file1.js file1.js

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

file2.js file2.js

Ti.App.fireEvent('customEventName');

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

file1.js file1.js

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

file2.js 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.

Hope this will help. 希望这会有所帮助。

this might be a problem of code structure. 这可能是代码结构的问题。 Basically you have three good way to doing this depending on which version you are using (actually on which version you started your project : 基本上,根据您使用的版本,您有三种好方法可以执行此操作(实际上您在哪个版本上启动了项目:

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM