Skip to content

Instantly share code, notes, and snippets.

@iOnline247
Created April 14, 2014 19:03
Show Gist options
  • Save iOnline247/10674795 to your computer and use it in GitHub Desktop.
Save iOnline247/10674795 to your computer and use it in GitHub Desktop.
var $1_10_2 = jQuery.noConflict();
(function( $, undefined ) {
var results = []
;
retrieveListItems();
function retrieveListItems() {
var cachedItems = JSON.parse(localStorage.getItem("pl-Vendors"));
if(!cachedItems) {
$.ajax({
url: "/sites/siteName/listName/Vendors.csv",
dataType: "text"
})
.done(parseCSV)
.fail(errorCSV);
} else {
$(function () {
renderTypeAhead(cachedItems);
});
}
}
function parseCSV(data, status) {
// Split on line feeds.
var items = data.split(/\n/),
headers,
headersLength
;
// Get rid of empty array on the end.
items.pop(items.length -1);
headers = items.shift();
if(!headers) {
return;
}
headers = headers.split(",");
headersLength = headers.length;
// console.dir(headers);
// console.dir(fields.length);
var itemsLength = items.length;
// Loop all of the items.
for (var i = 0; i < itemsLength; i++) {
var item = items[i].split(",");
var output = {};
// Loop the header row and stuff the output
// with properties:value. headers[j]: item[j]
for(var j = 0; j<headersLength; j++) {
output[headers[j]] = item[j];
}
// Used for typeahead.js
output.value = output.VendorName;
output.tokens = [output.VendorName, output.VendorID];
// For each item, push into results array.
results.push(output);
}
localStorage.setItem("pl-Vendors", JSON.stringify(results));
$(function () {
renderTypeAhead(results);
});
}
function errorCSV(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}
}($1_10_2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment