Drupal services JS
Component ID
1861750
Component name
Drupal services JS
Component type
module
Maintenance status
Development status
Component security advisory coverage
covered
Downloads
708
Component created
Component changed
Component body
NOTE: This is not a module, it's a simple javascript library to interact with services endpoints. It has no dependencies, and is really lightweight.
This can be used to integrate your Drupal site with a phonegap (Apache cordova) app.
Usage
var drupal = new drupalService();
drupal.url = 'http://example.com/endpoint';
drupal.method = 'node.json';
drupal.get();
That would give you the front page in json.
To log in:
var drupal = new drupalService();
// Set URL to your services endpoint.
drupal.url = 'http://example.com/endpoint';
// Set method to a services resource.
drupal.method = 'user/login.json';
// Set data to what you want to send (if anything).
// This example is sending username and password for logging in.
drupal.data = {
'username': 'admin',
'password': 'admin'
}
// Set callback to do something when you are done.
drupal.callback = function(data) {
console.log(data);
}
// Optionally set drupal.error to be an error callback. Syntax as above.
// In the end, either post or get (depending on what you are doing). Let's post this.
drupal.post();
A more advanced example with posting images in nodes:
var file = new drupalService();
file.url = 'http://dcoslo/endpoint';
// Data object for a file entity.
file.data = {
'file': 'LONG_BASE64_ENCODED_STRING',
'filename': 'test.png'
}
file.method = 'file.json';
file.callback = function(data) {
// Initialize new ajax request in callback function.
var node = new drupalService();
node.url = 'http://dcoslo/endpoint';
node.method = 'node.json';
// This data object is the node object.
node.data = {
'title': 'node test med bilde',
// Node type is article in this case.
'type': 'article',
// The image field is called field_image
'field_image': {
// I really hate seeing 'und' in code, but quick example here.
'und': {
0: {
// data.fid is the file id we got from first ajax request.
'fid': data.fid
}
}
}
}
node.callback= function(data) {
// Just logging the data from posting the node.
console.log(data);
}
// Post in the callback function (the node).
node.post();
}
// Post the file.
file.post();
