[英]Titanium, calling a function in one JS file from another JS file
所以我在過去的3天里一天都在嘗試了幾個小時。我研究了這個到死,但仍然無法得到它。
目標:
-file1.js有一個按鈕,按下時會調用file2.js中的方法Main_Menu,並打開由該方法或函數創建的新窗口。
嘗試失敗:
-i已經嘗試了Ti.include但總是得到一個,無法找到文件錯誤,我已經嘗試將字符串更改為每個可能的路徑。
-var file = require(path)但不能使用文件內的方法,例如file.Main_Meue,不起作用
我還嘗試了許多其他不想到的事情,但如果有人有任何建議或者您需要更多信息,請詢問。 請幫助,謝謝
第二個答案
像這樣創建第二個窗口:
//file1.js
button.addEventListener('click', function()
{
var secondWindow = Ti.UI.createWindow({
url:'file2.js'
});
secondWindow.open();
});
file1.js
通過url
參數使用file2.js
創建一個新窗口。 調用secondWindow.open()
后, file2.js
現在是你的新窗口
第一個答案
基於本主題的標題,您可以使用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
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.
希望這會有所幫助。
這可能是代碼結構的問題。 基本上,根據您使用的版本,您有三種好方法可以執行此操作(實際上您在哪個版本上啟動了項目:
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.