From ad19edc9b587b2ed527d73b13e43d270cc9dbf52 Mon Sep 17 00:00:00 2001 From: Christoph Date: Wed, 6 Mar 2013 13:27:36 +0100 Subject: [PATCH] implement timeago('update', 'datestr') function refactor initializer function to support different named functions refactor init to use single each loop like the other functions --- jquery.timeago.js | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/jquery.timeago.js b/jquery.timeago.js index 54da8af..fb00723 100644 --- a/jquery.timeago.js +++ b/jquery.timeago.js @@ -117,16 +117,35 @@ } }); - $.fn.timeago = function() { - var self = this; - self.each(refresh); + // functions that can be called via $(el).timeago('action') + // init is default when no action is given + // 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; - if ($s.refreshMillis > 0) { - setInterval(function() { self.each(refresh); }, $s.refreshMillis); - } - return self; - }; + $.fn.timeago = function(action, options) { + var fn = action ? functions[action] : functions.init; + if(!fn){ + throw new Error("Unknown function name '"+ action +"' for timeago"); + } + // each over objects here and call the requested function + this.each(function(){ + fn.call(this, options); + }); + return this; + }; function refresh() { var data = prepareData(this);