Titanium by calling a function in one JS file from another JS file

therefore, I have tried many hours a day for the past 3 days. I investigated it to death, but still cannot get it.

Goal:

-file1.js has buttion, which when pressed will call the Main_Menu method in file2.js and open a new window created by this method or function.

unsuccessful attempts:

-i tried Ti.include, but always got a can not find file error, I tried changing the line to any possible path.

-var file = require (path), but cannot use the method inside the file, e.g. file.Main_Meue, does not work

I also tried many other things that do not come to mind, but if anyone has any tips or you need more information, just ask. PLEASE HELP AND THANKYOU

+4
source share
3 answers

second answer

Create a second window as follows:

//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. file2.js now your new window after calling secondWindow.open()

First answer

Based on the name of this section, you can use the fireEvent method. For instance:

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

+2
source

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. 

Hope this helps.

+1
source

this may be a code structure problem. Basically, you have three good ways to do this depending on the version you are using (actually, on which version you started your project:

Hope this helps.

0
source

Source: https://habr.com/ru/post/1487976/


All Articles