Ti.App.fireEvent 不工作;错误未捕获的类型错误:无法读取未定义的属性“App" [英] Ti.App.fireEvent not working; error Uncaught TypeError: Cannot read property 'App' of undefined

查看:26
本文介绍了Ti.App.fireEvent 不工作;错误未捕获的类型错误:无法读取未定义的属性“App"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的 Titanium 应用程序,它打开一个 web 视图并加载一个 URL.我希望链接在默认浏览器中打开,而不是在应用中打开.

I have a simple Titanium app which opens a webview, and loads a URL. I want the links to open in the default browser, not the app.

我有一些 jQuery 代码,它为页面中的所有链接添加了一个点击事件.

I have some jQuery code which adds a click event to all links in the page.

<script>
jQuery('a').click(function() {
    console.log("Handler for .click() called for URL: " + this.href);
    Ti.App.fireEvent('ynOpenURL', {url:  this.href });
});

在 app.js 文件中我有:

In the app.js file I have:

var win = Ti.UI.createWindow();

var mywebview = Ti.UI.createWebView({
    url :  'http://xxxx.xxxxx.com/',
    enableZoomControls: false   
});

win.add(mywebview);
win.open();
Ti.App.addEventListener('ynOpenURL', function(e) {
 Ti.API.info('yyyyyy');
 Ti.API.info(e.url);

 if (Ti.Platform.canOpenURL(e.url)){
  Ti.Platform.openURL(e.url);
   } else {
    alert("Cannot open URL");
 }

当我在 Android (4.4) 上运行该应用程序时,网页加载正确.

When I run the app on my Android (4.4) the web page loads correctly.

当我点击手机上的链接时,我会在控制台中看到以下内容:

When I click a link on the phone I get the following in the console:

Handler for .click() called for URL: http://xxx.xxxxx.xxx/
Uncaught TypeError: Cannot read property 'App' of undefined (xxxx)

不记录任何 Titanium 控制台事件,仅记录网页中的 javascript 事件.

None of the Titanium console events are logged, only the javascript events in the webpage.

推荐答案

正如你提到的 webview 正在加载远程 url.因此 Ti.*Titanium.* 在您的远程服务器上不可用.

As you mentioned that the webview is loading remote url. So Ti.* or Titanium.* are not available at your remote server.

你能做的是:

  • 在应用内创建一个 html 文件并通过 ajax 加载数据并使用 fireEvent.
  • 通过titanium http客户端加载数据并相应地使用Ti.Platform.openURL.
  • Create a html file within the app and load the data via ajax and use fireEvent.
  • Load the data via titanium http client and use Ti.Platform.openURL accordingly.

引用自 Titanium WebView 文档

远程脚本

从远程 Web 服务器下载的脚本无法访问 Titanium 命名空间.

Scripts downloaded from remote web servers cannot access the Titanium namespace.

要与远程内容进行交互,请等待内容加载完毕,然后使用 evalJS 方法在 Web 视图中执行 JavaScript 表达式并检索表达式的值.

To interact with remote content, wait until the content is loaded, then use the evalJS method to execute a JavaScript expression inside the web view and retrieve the value of an expression.

这篇关于Ti.App.fireEvent 不工作;错误未捕获的类型错误:无法读取未定义的属性“App"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆