1

我有一个大型 HTML5 应用程序,并且正在使用 Titanium 为其添加本机支持。对我来说最简单的 hello world 是:

var webview = Titanium.UI.createWebView({url:'http://myApp.com'});
var window = Titanium.UI.createWindow();
window.add(webview);
window.open();

但是,如果我尝试在现有代码中使用 Titanium API,它将没有参考。是否有一个配置文件选项可以使 Titanium API 可用于我的 Web 应用程序?

我刚刚在文档中找到了这个:

Scripts downloaded from remote web servers cannot access the Titanium namespace, 
however, you can use the web view evalJS method to execute a JavaScript 
expression inside the web view, and retrieve the value of an expression.

除了轮询 webview 的上下文之外,还有什么方法可以让上下文访问 Titanium API?

4

3 回答 3

1

节省一些时间并使用 phonegap,您所做的并不是 Appcelerator 的最佳实践

于 2013-03-29T23:50:22.170 回答
1

我同意 Aaron 的观点,PhoneGap 听起来确实更适合您尝试做的事情。但是,如果您仍然想在 Titanium 中执行此操作......

简短的回答是您必须从 webview 运行 Ti.App.fireEvent(),这将允许您从 Titanium javascript 文件运行函数调用。在 Titanium javascript 文件中,您将使用 Ti.App.addEventListener 添加一个事件侦听器。

对于更详细的示例,我们需要更多上下文。

于 2013-04-01T16:01:11.680 回答
0

使用 web-view 将所有 html 文件加载到不同的 .JS 文件中并尝试执行此代码

Applicationwindow.js 文件,如果您想要 android 选项菜单,请保留此代码,否则请修改

根据你的事件。

功能应用程序窗口(){

//declare module dependencies

var All = require('ui/common/All');

    Tree = require('ui/common/Tree');

    EBOM = require('ui/common/E-BOM');

    MBOM = require('ui/common/M-BOM');

    SBOM = require('ui/common/S-BOM');

//create object instance

var self = Ti.UI.createWindow({

    title:'Products',

    exitOnClose:true,

    navBarHidden:true,

    backgroundColor:'#ffffff',
    /////////////////////////////////////////////////////////////////////////////

    activity: {

        onCreateOptionsMenu: function(e) {

            var menu = e.menu;              

            var menuItem = menu.add({ title: "C-BOM", icon: 'Arrow-Hover.jpg' });

            //menuItem.setIcon("Arrow-Hover.jpg");

            menuItem.addEventListener("click", function(e) {

                 var all = new All();

                        self.add(all);

            });



            var menuItem = menu.add({ title: "ALL-BOM" });

            menuItem.setIcon("images/refresh_icon.png");

            menuItem.addEventListener("click", function(e) {

                   var tree = new Tree();

                        self.add(tree);

            });


            var menuItem = menu.add({ title: "E-BOM" });

            menuItem.setIcon("images/refresh_icon.png");

            menuItem.addEventListener("click", function(e) {

                    var ebom = new EBOM();

                        self.add(ebom);

            });

            var menuItem = menu.add({ title: "M-BOM" });

            menuItem.setIcon("images/refresh_icon.png");

            menuItem.addEventListener("click", function(e) {

                    var mbom = new MBOM();

                        self.add(mbom);

            });

            var menuItem = menu.add({ title: "S-BOM" });

            menuItem.setIcon("images/refresh_icon.png");

            menuItem.addEventListener("click", function(e) {

                   var sbom = new SBOM();

                        self.add(sbom);

            });


            var menuItem = menu.add({ title: "Logout" });

            menuItem.setIcon("Arrow-Hover.jpg");

            menuItem.addEventListener("click", function(e) {

                 alert("Logout");

            });

           }

          }

    /////////////////////////////////////////////////////////////////////////////

});

var webview = Titanium.UI.createWebView({

    url:'/ui/common/Login.html'

    });

self.add(webview);


return self;

};

module.exports = 应用程序窗口;

于 2013-05-10T09:31:34.217 回答