Skip to content

Instantly share code, notes, and snippets.

@scottfalconer
Created October 21, 2012 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scottfalconer/3928627 to your computer and use it in GitHub Desktop.
Save scottfalconer/3928627 to your computer and use it in GitHub Desktop.
PNWDS presentation: code samples
// Create a new node object
var node = {
node:{
title: 'hello',
type:'post',
field_post_description: {
und: [{
value: 'This is a custom field value',
format: 'full_html'
}]
},
field_post_photo: {
und: [{
fid: 555,
}]
},
uid: 1,
}
};
var createNodeSuccess = function(response, data) {
alert('That worked!');
}
var createNodeError = function(response, error) {
// Something went wrong. Check console.log/Drupal watchdog/etc
alert('That did not work!');
}
var createNodeStatus = function(success, data) {
}
drupalNode.createNode({
node: node,
success: createNodeSuccess,
error: createNodeError,
status: createNodeStatus
});
exports.createNode = function(args) {
// Include the jQuery $.ajax clone tiajax.js library
Ti.include("tiajax.js");
// Define $ and $.ajax
$ = {}
$.ajax = Titanium.Network.ajax
// Define the URL to register this user
var url = drupalConfig.restPath + "node.json";
// Use $.ajax to create the comment
$.ajax({
type: "POST",
url: url,
data: JSON.stringify(args.node), // Stringify the comment
dataType: 'json',
contentType: 'application/json',
success: function(data, status) {
// todo: MAke sure we're actually mapping node create success and not just connection success
args.success(status, data);
},
error: function(error) {
var response = '';
args.error(response, error);
}
});
}
var DrupalUserUrl = drupalConfig.restPath + 'system/connect.json';
// Create a connection
var xhr = Titanium.Network.createHTTPClient();
xhr.setRequestHeader('Content-Type','application/json; charset=utf-8');
// Open the connection
xhr.open("POST", DrupalUserUrl);
// Send the connection
xhr.send();
xhr.onerror = function(e) {
// Something went wrong.
args.error(e);
};
// When the connection loads we do:
xhr.onload = function() {
var response = xhr.responseText;
var result = JSON.parse(response);
// Save the status of the connection in a variable
// this will be used to see if we have a connection (200) or not.
var statusCode = xhr.status;
// Check if we have a connection
if (statusCode == 200) {
// The user is successfully authenticated.
args.success(result);
}
else {
// Something went wrong.
args.error(statusCode);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment