Skip to content

Commit ad19edc

Browse files
author
Christoph
committed
implement timeago('update', 'datestr') function
refactor initializer function to support different named functions refactor init to use single each loop like the other functions
1 parent db9651b commit ad19edc

File tree

1 file changed

+28
-9
lines changed

1 file changed

+28
-9
lines changed

jquery.timeago.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,35 @@
117117
}
118118
});
119119

120-
$.fn.timeago = function() {
121-
var self = this;
122-
self.each(refresh);
120+
// functions that can be called via $(el).timeago('action')
121+
// init is default when no action is given
122+
// functions are called with context of a single element
123+
var functions = {
124+
init: function(){
125+
var refresh_el = $.proxy(refresh, this);
126+
refresh_el();
127+
var $s = $t.settings;
128+
if ($s.refreshMillis > 0) {
129+
setInterval(refresh_el, $s.refreshMillis);
130+
}
131+
},
132+
update: function(time){
133+
$(this).data('timeago', { datetime: $t.parse(time) });
134+
refresh.apply(this);
135+
}
136+
};
123137

124-
var $s = $t.settings;
125-
if ($s.refreshMillis > 0) {
126-
setInterval(function() { self.each(refresh); }, $s.refreshMillis);
127-
}
128-
return self;
129-
};
138+
$.fn.timeago = function(action, options) {
139+
var fn = action ? functions[action] : functions.init;
140+
if(!fn){
141+
throw new Error("Unknown function name '"+ action +"' for timeago");
142+
}
143+
// each over objects here and call the requested function
144+
this.each(function(){
145+
fn.call(this, options);
146+
});
147+
return this;
148+
};
130149

131150
function refresh() {
132151
var data = prepareData(this);

0 commit comments

Comments
 (0)