简体   繁体   English

如何在Titanium appcelerator中远程包含一个javascript文件并使用该文件的功能?

[英]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. 我需要将包含在特定链接上的javascript文件远程包含到我的appcelerator项目中,并使用该文件中声明的函数来处理一些数据。

What i would like to achieve is something like the following in html - 我想实现的是类似于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 // someMethod()在远程可用的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. 我是Appcelerator的一名新手,我无法真正追踪我遇到的一些线程,因此,非常感谢您提供一些详细的帮助。 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. 您现在要包含托管在某个服务器上的远程文件,由于Titanium代码在编译时会转换为本机代码,因此您不能包含来自远程文件的Titanium API。
  2. If you want to include a remote file , then only option which I see is loading that file in webview . 如果要包括远程文件,则只有看到的选项是将文件加载到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. 现在谈到您的问题,正如您所说的,您只想通过从远程文件中触发一些JS函数来仅从远程服务器中获取一些数据。 So following is what would I do :- 所以下面是我该怎么做:-

a/ Create a hidden webview in my main window with a EventListener of webview. A /创建一个隐藏的WebView具有的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 : b /在localHtmlFile.html文件中:

<!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 PS:这只是一种逻辑,您必须根据需要编辑代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在appcelerator Titanium中将WKWebView与本地文件一起使用? - How to use WKWebView with local file in appcelerator Titanium? 如何在Greasemonkey脚本中包含远程javascript文件? - How do I include a remote javascript file in a Greasemonkey script? 如何在 Appcelerator Titanium 中使用 CoffieeScript 而不是 JavaScript - How to use CoffieeScript in Appcelerator Titanium instead of JavaScript 如何在Titanium Appcelerator中创建XLS文件? - How to create a xls file in titanium appcelerator? 从 Appcelerator Titan 中的远程 XML 文件获取数据 - Getting data from a remote XML file in Appcelerator titanium appcelerator titanium:创建一个新文件 - appcelerator titanium: Creating a new file 如何在另一个 JavaScript 文件中包含一个 JavaScript 文件? - How do I include a JavaScript file in another JavaScript file? 如何在Titanium Studio(在index.js中)中包含.js文件? - How do I include a .js file in Titanium studio(in index.js)? Appcelerator Titanium-openFileChooserDialog不存在:如何打开对话框浏览文件? - Appcelerator Titanium - openFileChooserDialog does not exist: How to open a dialog to browse for a file? 如何有条件地包含远程JavaScript文件? - How to include a remote JavaScript file conditionally?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM