• Stars
    star
    197
  • Rank 190,993 (Top 4 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated about 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

RestAPI Sync Adapter for Titanium Alloy Framework

napp.alloy.adapter.restapi

RestAPI Sync Adapter for Titanium Alloy Framework.

Response Codes

The adapter has been designed with the following structure.

  • 200: The request was successful.
  • 201: The resource was successfully created.
  • 204: The request was successful, but we did not send any content back.
  • 304: The request was not modified.
  • 400: The request failed due to an application error, such as a validation error.
  • 401: An API key was either not sent or invalid.
  • 403: The resource does not belong to the authenticated user and is forbidden.
  • 404: The resource was not found.
  • 500: A server error occurred.

How To Use

Download restapi.js to PROJECT_FOLDER/app/assets/alloy/sync/ or use NPM thanks to appc-npm:

npm install alloy-sync-restapi --save

Add the following to your model in PROJECT_FOLDER/app/models/.

exports.definition = {
	config: {
		"URL": "http://example.com/api/modelname",
		//"debug": 1,
		"adapter": {
			"type": "restapi",
			"collection_name": "MyCollection",
			"idAttribute": "id"
		},
		"headers": { // your custom headers
            "Accept": "application/vnd.stackmob+json; version=0",
	        "X-StackMob-API-Key": "your-stackmob-key"
        },
        "parentNode": "news.domestic" //your root node
	},		
	extendModel: function(Model) {		
		_.extend(Model.prototype, {});
		return Model;
	},
	extendCollection: function(Collection) {		
		_.extend(Collection.prototype, {});
		return Collection;
	}		
}

Use the debug property in the above example to get logs printed with server response to debug your usage of the restapi adapter.

Lets see this in action

In your Alloy controller, do would use the REST API adapter like this:

var collection = Alloy.createCollection("MyCollection"); //or model
//the fetch method is an async call to the remote REST API.
collection.fetch({
	success : function(){
		_.each(collection.models, function(element, index, list){
			// We are looping through the returned models from the remote REST API
			// Implement your custom logic here
		});
	},
	error : function(){
		Ti.API.error("hmm - this is not good!");
	}
});

Another example is that,

// This is the handle for the item
var model = Alloy.createModel("MyCollection");

var params = {
    field1: "some field",
    fied2: "another field"
};

//the fetch method is an async call to the remote REST API.
model.save(params, {
    success : function(model) {
        Ti.API.log("Yay! Success!");
        Ti.API.log(model);
    },
    error : function(err) {
        Ti.API.error("hmm - this is not good!");
        Ti.API.error(err);
    }
});

Under the hood, this API uses the Backbone JS sync functionality. To have a solid understading of this libary, it will valuable to understand how does BackboneJS manages CRUD operations.

Special Properties

Custom Headers

Define your own custom headers. E.g. to add a BaaS API

"headers": {
	"Accept": "application/vnd.stackmob+json; version=0",
	"X-StackMob-API-Key": "your-stackmob-key"
}

Nested Result Objects

Lets say you have a REST API where the result objects are nested. Like the Twitter search API. It has the found tweets in a results object. Use the parentNode to specify from which root object you want to parse children objects.

config: {
	...
	"parentNode" : "results"
}

It has support for nested objects.

config: {
	...
	"parentNode" : "news.domestic"
}

Since v1.1.1 - you can specify this object as a function instead to custom parse the feed. Here is an example:

Feed: http://www.google.com/calendar/feeds/[email protected]/public/full?alt=json&orderby=starttime&max-results=15&singleevents=true&sortorder=ascending&futureevents=true

Custom parsing:

parentNode: function (data) {
	var entries = [];

	_.each(data.feed.entry, function(_entry) {
		var entry = {};

		entry.id = _entry.id.$t;
		entry.startTime = _entry.gd$when[0].startTime;
		entry.endTime = _entry.gd$when[0].endTime;
		entry.title = _entry.title.$t;
		entry.content = _entry.content.$t;

		entries.push(entry);
	});

	return entries;
}

ETag

This feature will only work if your server supports ETags. If you have no idea what this is, then consult your server admin. Start be enabling this feature in the model config, like the following:

config: {
	...
	"eTagEnabled" : true
}

You do not have to do anything more. The adapter will send and recieve the ETag for every single request and store those locally in the Ti.App.Properties namespace.

The adapter uses the IF-NONE-MATCH header to send the newest ETag for the provided url to the server on each request. Once a succesful response is recieved by the adapter, it will store the new ETag automatically.

Notice: This may be a good idea not to use this while developing, because it will cache and store your ETag - which might end up in wrong situations while you are working

Fetch data from POST requests

@Since v1.1.11 you can send requestMethod to fetch data from POST service

myModel.fetch({
	requestMethod: 'POST' // GET is default
})

Changelog

v1.1.13
BugFix for no responseText from a successful response

v1.1.12
Allow pass urlparams for model.destroy()

v1.1.11
Now you can set http request method in fetch, property name is requestMethod

v1.1.10
Bugfix for http headers after xhr.open() but before xhr.send()

v1.1.8
Bugfix for model.id #64 parentNode can be defined as a function #63

v1.1.5
Added ETag support
Bugfix for urlparams #34

v1.1.4
Added search mode

v1.1.3
Added support for accessing the error object. Issue #29 Thanks @alexandremblah

v1.1.2
JSON.parse errors are now caught. Thanks @FokkeZB

v1.1.1
Added support parentNode as a function for custom parsing. thanks @FokkeZB

v1.1.0
Added support for Nested Result Objects
Added support for Custom Headers
Code cleanup

v1.0.6
Added support for idAttribute

v1.0.5
Added HTTP Response code and error message

v1.0.4
Added debug

v1.0.3
Alloy 1.0.0.
Fix bug in rest url being global

v1.0.2
Added urlparams

v1.0.1
Android bugfixes

v1.0
init

Author

Mads Møller
web: http://www.napp.dk
email: [email protected]
twitter: @nappdev

License

The MIT License (MIT)

Copyright (c) 2010-2013 Mads Møller

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

More Repositories

1

TiSocial.Framework

iOS6+ Social.Framework. Appcelerator apps are able to share content to Facebook and Twitter.
Objective-C
259
star
2

NappDrawer

A side drawer navigation container view controller for Appcelerator Titanium.
Objective-C
248
star
3

napp.alloy.adapter.restsql

SQL & RestAPI Sync Adapter for Titanium Alloy Framework
JavaScript
144
star
4

NappSlideMenu

a new UI component for Appcelerator Titanium
Objective-C
128
star
5

NappUI

A collection of extended functionality for the UI components of Titanium SDK
Objective-C
121
star
6

NappAppearance

Use the power of iOS UIAppearance on Titanium UI components
Objective-C
69
star
7

NappDownloadManager

Download Manager for Appcelerator Titanium
Java
61
star
8

NappTestFlight

TestFlight for appcelerator
Python
43
star
9

NappImageView

iOS ImageView that supports ContentModes: AspectFit, AspectFill and Center
Objective-C
42
star
10

NappPDFCreator

This iOS module lets you generate PDF files from HTML or URL.
Objective-C
31
star
11

NappScrollViewExtended

TiUIScrollView extended with Pull To Refresh and Infinite Scrolling functionalities.
Objective-C
30
star
12

NappWaveformView

iOS module for titanium - Shows a graph of an audio file
Objective-C
29
star
13

GitLab-CI-Scripts

My Gitlab CI scripts
29
star
14

Alloy-Widgets-Adapters

List of Titanium Alloy Widgets and Adapters. Please send me your repo!
28
star
15

AudioPlayerWidget

Alloy widget for playing audio
JavaScript
20
star
16

NappSearchBarExtended

TiUISearchBar extended. Gives the developer more freedom to style the UI component
Python
20
star
17

NappFlashLight

Flashlight Titanium module for iOS and Android.
Python
19
star
18

Napp2DScrollView

Android bidirectional ScrollView for Appcelerator
Java
17
star
19

NappJockey

Communication between Titanium and webpage running remotely
Java
17
star
20

NappBugsnag

Bugsnag for Appcelerator Titanium
C
15
star
21

NappUrbanAirshipGCM

Urban Airship with Google Cloud Messaging support
Java
10
star
22

NappNUI

NUI for Appcelerator Titanium. NUI is a css styling UI Kit for iOS.
Objective-C
6
star
23

AlloyAdapterTemplate

Alloy Adapter StarterKit
JavaScript
5
star
24

mac-setup

setup a macOS quickly using brew and cask
Ruby
5
star
25

webhooks

Simple and powerful Webhooks for Laravel
PHP
5
star
26

lambda-office-to-pdf-processor

Convert Office file to PDF at scale
JavaScript
4
star
27

otp

One Time Password for Laravel
PHP
3
star
28

laravel-octane-test

Lets test Laravel Octane
PHP
3
star
29

pdftron-php-docker

PDFTron Docker image for PHP
Dockerfile
2
star
30

async-aws-laravel

test repo for laravel integration with Async AWS
PHP
1
star
31

Dock

Local Laravel Development based on Docker
Shell
1
star