简体   繁体   English

钛合金和Google Cloud Endpoint

[英]Alloy Titanium & Google Cloud Endpoint

I would like to know how (the right way) to work with Google Cloud Endpoint in an Alloy Titanium application. 我想知道如何(正确的方式)在Alloy Titanium应用程序中使用Google Cloud Endpoint。 And I would like to use the library that Google has for the API endpoints. 我想将Google拥有的库用于API端点。

I am new to Alloy and CommonJS, therefore trying to figure out the right way to do this. 我是Alloy和CommonJS的新手,因此尝试找出正确的方法。

From my understanding Alloy prefers (or only allows) including javascript via modules (CommonJS - exports...). 据我了解,Alloy偏爱(或仅允许)通过模块(CommonJS-导出...)包含javascript。

var module = require('google.js');
google.api.endpoint.execute();

This would be the way CommonJS would expect things to work. 这就是CommonJS期望事情正常进行的方式。 Although in the google javascript library it just creates a global variable called "gapi". 尽管在google javascript库中,它只是创建了一个称为“ gapi”的全局变量。

  • Is there a way, I can include this file ? 有没有办法,我可以包含此文件?
  • Is there a way, I can create global variables ? 有没有办法创建全局变量?
  • Should I stay away from creating them in first place ? 我应该远离最初的创建它们吗?

Thanks ! 谢谢 !

The client.js library that Google has for the API endpoints can be run only from browsers (Titanium.UI.WebView in this case), it can't be run directly from Titanium code since it contains objects not available in Titanium Appcelerator. Google为API端点提供的client.js库只能从浏览器(在本例中为Titanium.UI.WebView)运行,由于它包含Titanium Appcelerator中不可用的对象,因此无法直接从Titanium代码运行。

Also, using a Google Cloud Endpoint into an Alloy Titanium application requires having the js code available into the project at compile time, as it is used by Titanium to generate the native code for the desired platforms. 另外,在Alloy Titanium应用程序中使用Google Cloud Endpoint要求在编译时将js代码提供给项目,因为Titanium使用js代码生成所需平台的本机代码。

To anwser your questions: 要回答您的问题:

  • Is there a way, I can include this file ? 有没有办法,我可以包含此文件?

    1. No, if you plan to run the code as Titanium code, for the reasons mentioned above. 否,由于上述原因,如果您打算将代码作为Titanium代码运行。 Instead you could use the following code snippet to connect to a Google Cloud Endpoint: 相反,您可以使用以下代码段连接到Google Cloud Endpoint:

     var url = " https://1-dot-projectid.appspot.com/_ah/api/rpc "; var methodName = "testendpoint.listGreetings"; var apiVersion = "v1"; callMethod(url, methodName, apiVersion, { success : function(responseText) { //work with the response }, error : function(e) { //onerror do something } }); function callMethod(url, methodName, apiVersion, callbacks) { var xhr = Titanium.Network.createHTTPClient(); xhr.onload = function(e) { Ti.API.info("received text: " + this.responseText); if (typeof callbacks.success === 'function') { callbacks.success(this.responseText); } }; xhr.onerror = function(e) { Ti.API.info(JSON.stringify(e)); //Ti.API.info(e.responseText); if (typeof callbacks.error === 'function') { callbacks.error(e); } }; xhr.timeout = 5000; /* in milliseconds */ xhr.open("POST", url, true); xhr.setRequestHeader('Content-Type', 'application/json-rpc'); //xhr.setRequestHeader('Authorization', 'Bearer ' + token); var d = [{ jsonrpc: '2.0', method: methodName, id: 1, apiVersion: apiVersion, }]; Ti.API.info(JSON.stringify(d)); // Send the request. xhr.send(JSON.stringify(d)); } 

    1. Yes, if you use the embeded device's browser like this (as can be found in web client GAE samples) 是的,如果您使用这种嵌入式设备的浏览器(可以在Web客户端GAE示例中找到)
       webview = Titanium.UI.createWebView({ width : '100%', height : '100%', url : url // put your link to the HTML page }); 
      , to call your server HTML page which should contain: ,以调用服务器HTML页面,其中应包含:
       script src="https://apis.google.com/js/client.js?onload=init"> 
  • Is there a way, I can create global variables ? 有没有办法创建全局变量?

Yes, insert into the app/alloy.js file the global variables, see the default comments in the file: 是的,将全局变量插入app / alloy.js文件,请参阅文件中的默认注释:


    // 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";
    

  • Should I stay away from creating them in first place ? 我应该远离最初的创建它们吗?

I suppose you are reffering to global variables containing the module code for connecting to GAE enpoind methods. 我想您正在引用包含用于连接到GAE enpoind方法的模块代码的全局变量。 It's your call, here is how you can use them. 是您的电话,这是如何使用它们的方法。

a) Create a file named jsonrpc.js in the app/lib folder of your Titanium project, put the following code into it, and move the function code from above as the function body: a)在Titanium项目的app / lib文件夹中创建一个名为jsonrpc.js的文件,将以下代码放入其中,然后从上方将函数代码作为函数体移动:

JSONRPCClient = function () {
};
JSONRPCClient.prototype = {
    callMethod : function (url, methodName, apiVersion, callbacks) {
      // insert the function body here
    }
};
exports.JSONRPCClient = JSONRPCClient;

b) Into app/alloy.js file define your global variable: b)在app / alloy.js文件中定义您的全局变量:

Alloy.Globals.JSONRPCClient = require('jsonrpc').JSONRPCClient;

c) Use it (eg. from your controller js files): c)使用它(例如从您的控制器js文件中):

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
    }
});

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

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