Javascript 钛合金;谷歌云端点

Javascript 钛合金;谷歌云端点,javascript,google-app-engine,appcelerator,google-cloud-endpoints,titanium-alloy,Javascript,Google App Engine,Appcelerator,Google Cloud Endpoints,Titanium Alloy,我想知道如何(正确的方式)在Alloy Tianium应用程序中使用Google Cloud Endpoint。我想使用Google为API端点提供的库 我是Alloy和CommonJS的新手,因此我试图找出正确的方法来实现这一点 据我所知,Alloy更喜欢(或只允许)通过模块(CommonJS-exports…)包含javascript 这将是CommonJS期望的工作方式。虽然它只是创建了一个名为“gapi”的全局变量 有没有办法,我可以包括这个文件 有没有办法创建全局变量 我应该首先远离

我想知道如何(正确的方式)在Alloy Tianium应用程序中使用Google Cloud Endpoint。我想使用Google为API端点提供的库

我是Alloy和CommonJS的新手,因此我试图找出正确的方法来实现这一点

据我所知,Alloy更喜欢(或只允许)通过模块(CommonJS-exports…)包含javascript

这将是CommonJS期望的工作方式。虽然它只是创建了一个名为“gapi”的全局变量

  • 有没有办法,我可以包括这个文件
  • 有没有办法创建全局变量
  • 我应该首先远离创建它们吗

谢谢

Google为API端点提供的client.js库只能从浏览器(在本例中为Titanium.UI.WebView)运行,不能直接从Tianium代码运行,因为它包含Tianium Appcelerator中不可用的对象

此外,在Alloy Tianium应用程序中使用Google云端点需要在编译时将js代码提供给项目,因为Tianium使用js代码生成所需平台的本机代码

回答您的问题:

  • 有没有办法,我可以包括这个文件

  • 不,如果出于上述原因,您计划将代码作为钛代码运行。相反,您可以使用以下代码段连接到Google云端点:
    • 我应该首先远离创建它们吗
    我假设您正在引用包含用于连接GAE enpoind方法的模块代码的全局变量。这是你的决定,以下是你如何使用它们

    a) 在Tianium项目的app/lib文件夹中创建一个名为jsonrpc.js的文件,将以下代码放入其中,并将函数代码作为函数体从上面移动:

    JSONRPCClient = function () {
    };
    JSONRPCClient.prototype = {
        callMethod : function (url, methodName, apiVersion, callbacks) {
          // insert the function body here
        }
    };
    exports.JSONRPCClient = JSONRPCClient;
    
    b) 在app/alloy.js文件中定义全局变量:

    Alloy.Globals.JSONRPCClient = require('jsonrpc').JSONRPCClient;
    
    c) 使用它(例如,从控制器js文件):

    
    webview = Titanium.UI.createWebView({
                width : '100%',
                height : '100%',
                url : url // put your link to the HTML page
            });
    
    
    script src="https://apis.google.com/js/client.js?onload=init">
    
    
        // This is a great place to do any initialization for your app
        // or create any global variables/functions that you'd like to
        // make available throughout your app. You can easily make things
        // accessible globally by attaching them to the 
    Alloy.Globals
    // object. For example: // Alloy.Globals.someGlobalFunction = function(){}; Alloy.Globals.someGlobalVariable = "80dp";
    JSONRPCClient = function () {
    };
    JSONRPCClient.prototype = {
        callMethod : function (url, methodName, apiVersion, callbacks) {
          // insert the function body here
        }
    };
    exports.JSONRPCClient = JSONRPCClient;
    
    Alloy.Globals.JSONRPCClient = require('jsonrpc').JSONRPCClient;
    
    var client = new Alloy.Globals.JSONRPCClient();
    var url = "https://1-dot-projectid.appspot.com/_ah/api/rpc";
    var methodName = "testendpoint.listGreetings";
    var apiVersion = "v1";
    
    client.callMethod(url, methodName, apiVersion,
        {success: function(result) {
            //result handling
            Ti.API.info('response result=', JSON.stringify(result));
            //alert(JSON.stringify(result));
        },
        error: function(err) {
            Ti.API.info('response out err=', JSON.stringify(err));
            //error handling
        }
    });