4
votes

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.

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.

failed attempts:

-i have tried Ti.include but always getting a, cant find file error, i have tried changing string to every possible path.

-var file = require(path) but can not use the method inside the file, for example file.Main_Meue, does not work

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

3
Let me see if I understand your question. file1.js has a button. When you click the button, a new window is created using file2.js? Simply put Main_Menu(); at the end of file2.js and when the window is created, it will call that function - Ronnie
yes but main_menu(){ has a create window code }. so the code for the newly opened window is in file2.js, same as the method - Mikecit22
calling main_menu in file 2 is unnecessary. You create the window from file1. I'll update my answer - Ronnie

3 Answers

2
votes

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

First answer

Based off the title of this topic, you can use the fireEvent method. For example:

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
votes

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 will help.

0
votes

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.