implement timeago('update', 'datestr') function

refactor initializer function to support different named functions
refactor init to use single each loop like the other functions
This commit is contained in:
Christoph 2013-03-06 13:27:36 +01:00
parent db9651b797
commit ad19edc9b5

View file

@ -117,16 +117,35 @@
} }
}); });
$.fn.timeago = function() { // functions that can be called via $(el).timeago('action')
var self = this; // init is default when no action is given
self.each(refresh); // functions are called with context of a single element
var functions = {
init: function(){
var refresh_el = $.proxy(refresh, this);
refresh_el();
var $s = $t.settings;
if ($s.refreshMillis > 0) {
setInterval(refresh_el, $s.refreshMillis);
}
},
update: function(time){
$(this).data('timeago', { datetime: $t.parse(time) });
refresh.apply(this);
}
};
var $s = $t.settings; $.fn.timeago = function(action, options) {
if ($s.refreshMillis > 0) { var fn = action ? functions[action] : functions.init;
setInterval(function() { self.each(refresh); }, $s.refreshMillis); if(!fn){
} throw new Error("Unknown function name '"+ action +"' for timeago");
return self; }
}; // each over objects here and call the requested function
this.each(function(){
fn.call(this, options);
});
return this;
};
function refresh() { function refresh() {
var data = prepareData(this); var data = prepareData(this);