简体   繁体   中英

How do i remote include a javascript file in Titanium appcelerator and use the functions of that file?

I am required to remotely include into my appcelerator project, a javascript file available at a particular link, and use the function declared in that file to process some data.

What i would like to achieve is something like the following in html -

<script src="https://some-link/Data.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
var testVariable = someMethod(localdata);
});
//use testVariable as necessary
</script>

//someMethod() is declared in remotely available Data.js

I am a newb at Appcelerator and im not really able to follow some of the threads i have come across, so some detailed help would be really appreciated. Thank you in advance.

Well according to me , you should first understand few points first :

  1. You want to include a remote file hosted at some server , now as the Titanium code converts to native code at compile time , you cannot include Titanium API's from remote file.
  2. If you want to include a remote file , then only option which I see is loading that file in webview .

Now coming to your problem , as you said that you want to fetch some data only from remote server by triggering some JS function from remote file. So following is what would I do :-

a/ Create a hidden webview in my main window with a EventListener of webview. Something like :

var webview = Titanium.UI.createWebView({url:'localHtmlFile.html'});

//event listener to handle the response from webview
Ti.App.addEventListener('fromWebView', function(e) 
{ 
    var testVariable = e.data; 
});

b/ In localHtmlFile.html file :

<!DOCTYPE html>
<html>
<body>
     <script src="https://some-link/Data.js" type="text/javascript"></script>
     <script type="text/javascript">
     $(document).ready(function(){
          var testVariable = someMethod();
          //respond the fetch data to the main window via fireEvent
          Ti.App.fireEvent( 'fromWebView', { data : testVariable } );
     });
     </script>
</body>
</html>

PS : This is just a logic to begin with , you have to edit code according to your requirements

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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