您的方法不起作用,因为 timeago 通过 jQuery 的 data() 函数创建了一个缓存。因此,仅仅更新标题是不够的。
我认为自定义绑定是最好和最干净的方法:
ko.bindingHandlers.timeago = {
update: function(element, valueAccessor) {
var value = ko.utils.unwrapObservable(valueAccessor());
var $this = $(element);
// Set the title attribute to the new value = timestamp
$this.attr('title', value);
// If timeago has already been applied to this node, don't reapply it -
// since timeago isn't really flexible (it doesn't provide a public
// remove() or refresh() method) we need to do everything by ourselves.
if ($this.data('timeago')) {
var datetime = $.timeago.datetime($this);
var distance = (new Date().getTime() - datetime.getTime());
var inWords = $.timeago.inWords(distance);
// Update cache and displayed text..
$this.data('timeago', { 'datetime': datetime });
$this.text(inWords);
} else {
// timeago hasn't been applied to this node -> we do that now!
$this.timeago();
}
}
};
用法很简单:
<abbr data-bind="timeago: Posted"></abbr>
演示:http: //jsfiddle.net/APRGc/1/