Skip to content

Instantly share code, notes, and snippets.

@troyscott
Created December 27, 2013 05:14
Show Gist options
  • Save troyscott/8142826 to your computer and use it in GitHub Desktop.
Save troyscott/8142826 to your computer and use it in GitHub Desktop.
Example of using jQuery to parse timesheet data returned from nodejs podio API wrapper. PathJS is used to provide some simple routing.
console.log('ready ...');
function listTimesheets() {
var timesheets = [];
$('#add').hide();
$.ajax({
type: "GET",
dataType: "json",
url: "/timesheets"}).
done(function(data, status) {
console.log(status);
console.log(data);
if ($(".item").length > 0) {
$(".item").remove();
}
// iterate through timesheets
$.each(data, function(index, value){
// initiate local variables to for timesheet propreties
var itemId = "";
var title = "";
var timeSpent = "";
var rate = "";
var totalCost = "";
var description = "";
itemId = value.item_id;
// iterate through each property of a the podio timesheet
$.each(value, function(key, value) {
if (key == 'fields') {
$.each(value, function(index, value){
if (value.label == "Title") {
title = value.values[0].value;
}
if (value.label == "Time spent") {
timeSpent = value.values[0].value;
}
if (value.label == "Hourly rate") {
rate = value.values[0].value;
}
if (value.label == "Total cost") {
totalCost = value.values[0].value;
}
if (value.label == "Details of work") {
description = value.values[0].value;
}
});
} // if fields
}); // each property of a timesheet
var obj = {
itemId: itemId,
title: title,
timeSpent: timeSpent,
rate: rate,
totalCost: totalCost,
description: description
};
timesheets.push(obj);
}); // timesheets
$.each(timesheets, function(index, timesheet){
console.log(timesheet);
var items = []
items.push('<ul class="item" >');
editLink = '<a href="#/edit/' + timesheet.itemId + '" >'
items.push(editLink + '<h3>' + timesheet.title + '</h3></a>');
items.push('<li>Time: ' + timesheet.timeSpent + '</li>');
items.push('<li>Rate: ' + timesheet.rate + '</li>');
items.push('<li>Total: ' + timesheet.totalCost + '</li>');
items.push('<li>Description: ' + timesheet.description + '</li>');
items.push('</ul>');
$(items.join('')).appendTo('.timesheet');
});
}). // done
fail(function() {
console.log('fail');
});
} // listTimesheets
function addTimesheet() {
console.log('add timesheet ...');
$('#add').show();
if ($(".item").length > 0) {
$(".item").remove();
}
}
function home() {
console.log('home');
$('#add').hide();
if ($(".item").length > 0) {
$(".item").remove();
}
}
Path.map("#/home").to(home);
Path.map("#/list").to(listTimesheets);
Path.map("#/add").to(addTimesheet);
Path.map("#/edit/:item").to(function(){
alert("Timesheet: " + this.params['item']);
});
Path.root("#/home");
Path.listen();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment