How to access js file from another js file in android titanium studio project

I am new to mvc titanium studio and frame design studios. I hv two js files in the controller folder. one is index.js (created automatically when the project is created) and home.js. Now I want to open home.js on a button event from index.js (for example, start a new activity from another activity in the eclipse android application). Here is my code:

index.js:

function login_Click(e){ Ti.include('home.js'); hello(); } $.index.open(); 

where login_click (e) is the onClick event.

And home.js:

 function hello(){ //$.home.open(); alert("Opened"); } //exports.hello = hello; 

but whenever I run it and press the button, it gives an error

location: [25.1] Alloy / controllers / home.js

massage: incomprehensible reference error: module not defined

source: * module.export = controller;

Here is my alloy / controllers / home.js:

 function Controller() { require("alloy/controllers/BaseController").apply(this, Array.prototype.slice.call(arguments)); arguments[0] ? arguments[0]["__parentSymbol"] : null; arguments[0] ? arguments[0]["$model"] : null; var $ = this; var exports = {}; $.__views.home = Ti.UI.createWindow({ backgroundColor: "white", id: "home" }); $.__views.home && $.addTopLevelView($.__views.home); $.__views.label = Ti.UI.createLabel({ text: "Hell Yeah", id: "label" }); $.__views.home.add($.__views.label); exports.destroy = function() {}; _.extend($, $.__views); $.home.open(); _.extend($, exports); } var Alloy = require("alloy"), Backbone = Alloy.Backbone, _ = Alloy._; module.exports = Controller; 

Help me here. I tried the require () method. I tried to open it directly with $ .home.open (); But nothing happened. What do I need to do???? Thanx in advance ....

+4
source share
2 answers

You must use Alloy to do this to open the Home Controller view, just do the following:

 function login_Click(e){ var homeController = Alloy.createController('home'); // If home.xml container is a Window this will work homeController.getView().open(); } 
+4
source

Or, if you are trying to call the methods of another js file in another file, you need to export this function to use it. For instance,

home.js

 exports.myFunction = function(){ alert("I am in"); } 

index.js

 var home = require("home"); home.myFunction(); 

And there you go.

0
source

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


All Articles