-
-
Save 2bj/1977001 to your computer and use it in GitHub Desktop.
Demo CommonJS module for Titanium Mobile interaction with Drupal Services 3.x REST + JSON interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tested using: | |
Drupal 6.24 | |
Services 3.1 | |
Titanium Mobile SDK 1.8.2 (Android 2.2 SDK) | |
Services module configured as this example: | |
http://www.tylerfrankenstein.com/code/android-app-with-drupal-7-services-phonegap-and-jquery-mobile | |
Special thanks to @grzegorzbartman (www.openbit.pl) for coaching and inspiration. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ti.API.info('starting up...'); | |
var drupalServices = require('drupalServices'); | |
var REST_URL = 'http://192.168.1.102:8888/drupal/api/rest/'; | |
Ti.App.addEventListener('net:userLogoutReturned', function(data) { | |
Ti.API.info('userLogoutReturned: '+JSON.stringify(data)); | |
}) | |
Ti.App.addEventListener('net:userLoginReturned', function(data) { | |
Ti.API.info('userLoginReturned: '+JSON.stringify(data)); | |
}) | |
Ti.App.addEventListener('net:systemConnectReturned', function(data) { | |
Ti.API.info('systemConnectReturned: '+JSON.stringify(data)); | |
}) | |
Ti.App.addEventListener('net:nodeListReturned', function(data) { | |
Ti.API.info('nodeList returned: '); | |
for (var i = 0; i < data.length; i++) { | |
Ti.API.info('node '+i+':'+JSON.stringify(data[i])); | |
} | |
}); | |
drupalServices.userLogout(REST_URL); | |
drupalServices.userLogin('<drupal username>','<drupal password>',REST_URL); | |
drupalServices.systemConnect(REST_URL); | |
drupalServices.nodeList(REST_URL); | |
// give userLogin time to complete, then attempt createNode | |
setTimeout(function() { | |
Ti.API.info('3 seconds, create node!'); | |
// use Date() to create some pseudo-random numbers for title and body text tests | |
var date = new Date(); | |
drupalServices.createNode( | |
{node:{ | |
title: 'Titanium ' + date.getSeconds(), | |
type:'story', | |
body: 'body text, minutes: '+date.getMinutes(), | |
} | |
}, | |
REST_URL) | |
}, 3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nodeList = function(url) { | |
var xhr = Titanium.Network.createHTTPClient(); | |
xhr.open('GET', url+'node.json'); | |
xhr.send(); | |
xhr.onload = function() { | |
var jsonObject = JSON.parse(this.responseText); | |
Ti.App.fireEvent('net:nodeListReturned', jsonObject); | |
}; | |
xhr.onerror = function(e) { | |
// should do something more robust | |
Titanium.API.info('e.error'); | |
}; | |
}; | |
var systemConnect = function(url) { | |
var xhr = Titanium.Network.createHTTPClient(); | |
url = url + 'system/connect.json'; | |
Ti.API.info('url: '+url); | |
xhr.open('POST', url); | |
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); | |
xhr.send(); | |
xhr.onload = function() { | |
var jsonObject = JSON.parse(this.responseText); | |
// fire an app-level event to notify the UI that the JSON data is available | |
Ti.App.fireEvent('net:systemConnectReturned', { | |
data : jsonObject, | |
}); | |
}; | |
xhr.onerror = function(e) { | |
// should do something more robust | |
Titanium.API.info(e.error); | |
}; | |
}; | |
var userLogin = function(userName, userPass, url) { | |
// Pulls the JSON feed data and returns it to caller | |
var xhr = Titanium.Network.createHTTPClient(); | |
url = url + 'user/login.json'; | |
var user = { | |
username: userName, | |
password: userPass, | |
} | |
Ti.API.info('url: '+url); | |
xhr.open('POST', url); | |
xhr.setRequestHeader('Content-Type','application/json; charset=utf-8'); | |
var obj = JSON.stringify(user); | |
xhr.send(obj); | |
xhr.onload = function() { | |
var jsonObject = JSON.parse(this.responseText); | |
// fire an app-level event to notify the UI that the JSON data is available | |
Ti.App.fireEvent('net:userLoginReturned', jsonObject); | |
}; | |
xhr.onerror = function(e) { | |
// should do something more robust | |
Titanium.API.info('userLogin error: '+e.error); | |
}; | |
}; | |
var userLogout = function(url) { | |
// Pulls the JSON feed data and returns it to caller | |
var xhr = Titanium.Network.createHTTPClient(); | |
url = url + 'user/logout.json'; | |
Ti.API.info('url: '+url); | |
xhr.open('POST', url); | |
xhr.setRequestHeader('Content-Type','application/json; charset=utf-8'); | |
xhr.send(); | |
xhr.onload = function() { | |
var jsonObject = JSON.parse(this.responseText); | |
// fire an app-level event to notify the UI that the JSON data is available | |
Ti.App.fireEvent('net:userLogoutReturned', { | |
data : jsonObject, | |
}); | |
}; | |
xhr.onerror = function(e) { | |
// should do something more robust | |
Titanium.API.info('userLogout error: '+e.error); | |
}; | |
}; | |
var createNode = function(node, url) { | |
var xhr = Titanium.Network.createHTTPClient(); | |
url = url + 'node'; | |
Ti.API.info('creating node, url: '+url); | |
xhr.open('POST', url); | |
xhr.onload = function() { | |
var nodeObject = JSON.parse(this.responseText); | |
Ti.API.info('nodeObject: '+JSON.stringify(nodeObject)); | |
// fire an app-level event to notify the UI that the JSON data is available | |
Ti.App.fireEvent('net:createNodeReturned', { | |
data : nodeObject, | |
}); | |
}; | |
xhr.setRequestHeader('Content-Type','application/json; charset=utf-8'); | |
var obj = JSON.stringify(node); | |
Ti.API.info('node object: '+ obj); | |
xhr.send(obj); | |
xhr.onerror = function(e) { | |
// should do something more robust | |
Titanium.API.info('createNode error: '+e.error); | |
}; | |
}; | |
exports.createNode = createNode; | |
exports.userLogout = userLogout; | |
exports.userLogin = userLogin; | |
exports.nodeList = nodeList; | |
exports.systemConnect = systemConnect; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment