Skip to content

Instantly share code, notes, and snippets.

@scottdavis
Created March 25, 2010 20:52
Show Gist options
  • Save scottdavis/344094 to your computer and use it in GitHub Desktop.
Save scottdavis/344094 to your computer and use it in GitHub Desktop.
var Waterfall = function(container, test_mode) {
var self = {test_mode:true, timer:null, data:[], ajax_url:'get_data.php'};
self.init = function(container, test_mode) {
self.container = jQuery(container);
self.clear_contents();
self.init_timers();
return self;
};
self.clear_contents = function() {
self.container.empty();
};
self.init_timers = function(interval) {
var seconds = 3000;
if(interval){seconds = interval * 1000; }
self.poll();
self.timer = window.setInterval(self.poll, seconds);
};
self.poll = function() {
self.add_message(self.get_message());
};
self.add_message = function(json) {
if(!json) {return};
var t = jQuery.template("<div class='water' id='water_${id}'><div class='${service} icon'>&nbsp;</div><p>${message}</p></div>");
self.container.prepend(t.eval(json.brick))
var message = jQuery('#water_' + json.brick.id);
message.hide();
message.show('blind', { direction: "vertical" }, 500);
var children = self.container.children('.water');
console.log(children.length);
if(children.length > 2) {
self.remove_message(children.last());
}
};
self.remove_message = function(ele) {
ele = jQuery(ele);
ele.animate({height: 0}, function() {
$(this).remove();
});
};
self.get_message = function() {
if(self.data.length < 10) {
self.get_data();
}
var message = self.data.pop();
return message;
};
self.get_data = function() {
if(self.test_mode) {
self.data = water_fall_test_data().concat(self.data);
return;
}
$.getJSON(self.ajax_url, function(data){
self.data = self.get_data().concat(self.data);
});
}
return self.init(container, test_mode);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment