Appcelerator Blog

The Leading Resource for All Things Mobile

Appcelerator Arrow Client-Side SDK Example

28 Flares 28 Flares ×

This post provides a simple step-by-step example of generating and utilizing the Arrow Client-Side SDK for an Arrow application.

For this example, I will utilize a simple ArrowDB Arrow project and a simple HelloWorld Titanium app.

The Arrow Admin console enables you to test the APIs and also provides sample code to consume the API as shown below. As you can see, the Titanium code sample provided in the admin console shows how to manually make an HTTPClient web service call in order to retrieve the ArrowDB data. For this walkthrough, we will utilize the Client-Side SDK to retrieve this data instead.

ArrowDB Project

My simple Arrow project has an ArrowDB employee model shown below:

var Arrow = require('arrow');
var Model = Arrow.createModel('employee', {
    fields: {
        firstname: {
            type: String
        },
        lastname: {
            type: String
        },
        title: {
            type: String
        }
    },
    connector: 'appc.arrowdb',
    actions: [
        'create',
        'read',
        'update',
        'delete',
        'deleteAll'
    ]
});
module.exports = Model;

A sample FindAll response is shown below:

{
  "success": true,
  "request-id": "92b3cd5c-19da-447b-86f9-7236e5f57e3e",
  "key": "employees",
  "employees": [
    {
      "id": "572919b528ddb0090682772f",
      "firstname": "John",
      "lastname": "Doe",
      "title": "Account Representative"
    },
    {
      "id": "572919851cfadc0903834b44",
      "firstname": "Susan",
      "lastname": "French",
      "title": "VP WW Sales"
    },
    {
      "id": "572919741cfadc0903834b26",
      "firstname": "David",
      "lastname": "Glass",
      "title": "CEO"
    }
  ]
}

Generate the SDK

The Appcelerator Command Line tool now supports generating a client side SDK in order to ease client side integration of your Arrow APIs instead manually making an HTTPClient web service call.

Once you have a working Arrow project working and published (or running locally), it is time to generate the client SDK.

Type the following from the command line:

appc generate

Select “Arrow SDK” in the response:

? What type of component would you like to generate?
  Arrow Component
❯ Arrow SDK
  CLI Plugin

Then select the client SDK type:

? Which type of App SDK?
  Alloy
  Android
  jQuery
  NodeJS
  Objective-C
❯ Titanium

I chose Titanium for this example but you can see all the client SDK types that are currently supported.

Enter a name for the generated SDK (e.g.MyArrowApp).

? What should the name of the SDK be? MyArrowApp

Specify the folder to put the SDK into. I left this as default (sdk):

? Generate files into which directory? (sdk)

Look in your Arrow project folder and you will see the generated SDK, MyArrowApp.js.

The readme provided in the SDK folder provides excellent documentation on how to install and use the SDK, including how to take advantage of client side caching.

Installing and Using the SDK

In my example, I created a Default Alloy Project in Appcelerator Studio.

Then, in order to use the Titanium Client SDK created above, copy the MyArrowApp.js file to your project’s app/lib folder.

NOTE: You may need to create the app/lib folder

Then I copied and pasted the sample code from the Usage section of the sdk/readme.md file into the Titanium index.js controller file into the doCLick function as follows:

var MyArrowApp = require(‘MyArrowApp’);
MyArrowApp.port = 8080;
MyArrowApp.Authorization = ‘KXUJoLX58LuzNjWd7Y0lcdIYTr0R5xb6’;

var Employee = MyArrowApp.getModel(‘employee’);
Employee.findAll(function(err, results, client) {
  console.log(results);
});
“

Running the TItanum application and clicking on the “Hello World ” Label causes this code to execute and to display the ArrowDB employee data in the console as follows:

[INFO] :   [LiveView] Reloading App
[INFO] :   UI SHUTDOWN COMPLETE. TRYING TO RESUME RESTART
[INFO] :   HelloWorldLB/1.0 (5.2.2.b685ddb)
[INFO] :   (
[INFO] :           {
[INFO] :           firstname = John;
[INFO] :           id = 572919b528ddb0090682772f;
[INFO] :           lastname = Doe;
[INFO] :           title = "Account Representative";
[INFO] :       },
[INFO] :           {
[INFO] :           firstname = Susan;
[INFO] :           id = 572919851cfadc0903834b44;
[INFO] :           lastname = French;
[INFO] :           title = "VP WW Sales";
[INFO] :       },
[INFO] :           {
[INFO] :           firstname = David;
[INFO] :           id = 572919741cfadc0903834b26;
[INFO] :           lastname = Glass;
[INFO] :           title = CEO;
[INFO] :       }
[INFO] :   )

Summary

In this blog post we saw how Arrow can generate client side SDKs for your Arrow project so that the Arrow API’s can be accessed programmatically in your Titanium apps (as well as native, web, NodeJS, etc…). Code for this project can be found here.

28 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 28 Email -- 28 Flares ×

Sign up for updates!

Become a mobile leader. Take the first step to scale mobile innovation throughout your enterprise.
Get in touch
computer and tablet showing Appcelerator software
Start free, grow from there.
28 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 28 Email -- 28 Flares ×