Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var currentData = new Array();
var requestAgain = true;
var xhr = Titanium.Network.createHTTPClient();
var returnedData;
var timeDelay = (2 * 60 * 1000); // 2 mins * 60 seconds * 1000 milliseconds
// Setup our event handlers
xhr.onload = function() {
// This gets first one receipt of the data
returnedData = this.responseText;
// Let's turn the data from a string to a json dataset
// We use another variable - to handle exceptions when parsing the data
// so we don't lose the original data
var rDataJSON = JSON.parse(returnedData);
//
// Ok - we have the data - we now need to do something with it
processData(rDataJSON);
requestAgain = true;
};
xhr.onerror = function(e) {
Ti.API.info('GET returned error:' + e.error);
// You need to decide here what to do - potentially xhr is in an invalid state
// So it might need recreating
// Let's stop the timeout from running
clearInterval(timeoutHandler);
};
var timeoutHandler = setInterval(function() {
// This where we do the request
doAjaxRequest();
},timeDelay);
function doAjaxRequest() {
// Only request again - if we have completed sucessfully before
if (requestAgain) {
requestAgain = false;
// Ok - we should really be smart with this -
// we don't really want ALL the data returned everytime
// So you should talk with your server devs about passing a timestamp token
// which will allow the server only to respond with changed or new data
// Returned data should have an ID or key we can use to selectively update our data
xhr.open("GET","http://api.appcelerator.net/service?token=value");
xhr.send();
}
}
function processData(data) {
// Process the data
// I'm assuming that we have an array returned that has a string ID with each row
// We don't handle deleating data :)
for ( var i = 0; i < data.length; i++) {
var thisItem = data[i];
if (in_array(thisItem.ID, currentData)) {
// The item already exists
// You now need to decide what to do
// Some Ti UI controls have an update method
} else {
// The item is new
// You now need to decide what to do
// Some Ti UI controls have an append method
}
currentData[thisItem.ID] = thisItem;
}
}
// From: http://phpjs.org/functions/in_array:432
function in_array (needle, haystack, argStrict) {
// Checks if the given value exists in the array
//
// version: 1006.1915
// discuss at: http://phpjs.org/functions/in_array
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
// + improved by: vlado houba
// + input by: Billy
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
// * example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
// * returns 1: true
// * example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
// * returns 2: false
// * example 3: in_array(1, ['1', '2', '3']);
// * returns 3: true
// * example 3: in_array(1, ['1', '2', '3'], false);
// * returns 3: true
// * example 4: in_array(1, ['1', '2', '3'], true);
// * returns 4: false
var key = '', strict = !!argStrict;
if (strict) {
for (key in haystack) {
if (haystack[key] === needle) {
return true;
}
}
} else {
for (key in haystack) {
if (haystack[key] == needle) {
return true;
}
}
}
return false;
}
@dasher

This comment has been minimized.

Copy link
Owner Author

dasher commented Jul 9, 2010

The send accepts parameters as defined here so you would need to provide a parameter or access a variable to determine what to send.

A specific example of posting something to twitpic can be seen here

@dasher

This comment has been minimized.

Copy link
Owner Author

dasher commented Jul 9, 2010

If you're not seeing the "IN ONLOAD" in the log output then you'll need to reinit the xhr object - like this
You'll need to check the code for typos - I just changed in the gist - so something might have crept in.

You're not seeing any errors or warnings in the log output?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.