relic7
5/30/2014 - 6:44 AM

Simple jQuery ajax GET request

Simple jQuery ajax GET request

/*
 * Task 2 - fetch some db results via ajax call and display in a table.
 */
var admixt_task2 = {
	url: '/followers',

	fetch: function(){
		var self = this;

		$.get(this.url)
		  .done(function(data) {
				var json = JSON.parse(data);
				console.log(json);
				self.fillTable(json);
		  })
		  .fail(function(data) {
		  	// bootstrap error msg
		  });
	},

	fillTable: function(data) {

		var source   = $("#table-template").html();
		var template = Handlebars.compile(source);

		var test = {followers: data};

		var html = template(test);

		$('#results').html(html);
		$('#followers').dataTable();

		$('#trigger-task2').hide();
	}
}

$(document).ready(function(){
	$('#trigger-task2').click(function(){
		admixt_task2.fetch();
	});
});