I have a simple class, Event
with a computed property:
import moment from \'moment\';
export class Event
The latest aurelia release has a new feature: binding behaviors that will help with this use case.
First step would be to remove the playedFromNow
property from the view-model. We're going to put the logic in a value-converter to eliminate the dirty-checking and enable the logic to be reused in other views:
from-now.js
import moment from 'moment';
export class FromNowValueConverter {
toView(date) {
return moment(date).fromNow();
}
}
Now lets update our view to use the value converter as well as the built-in signal
binding behavior. With signal
we'll be able to tell the binding when to refresh.
Change ${event.playedFromNow}
to:
${event.CreateDate | fromNow & signal:'tick'}
In plain english this binding says, convert the date value using the fromNow
converter and refresh the binding whenever tick
is signaled.
Don't forget to import the value converter at the top of your view:
<!-- this goes at the top of any view using the FromNowValueConverter -->
<require from="./from-now"></require>
Finally, let's fire the tick
signal periodically... every minute?
import {BindingSignaler} from 'aurelia-templating-resources';
@inject(BindingSignaler)
export class App {
constructor(signaler) {
// refresh all bindings with the signal name "tick" every minute:
setInterval(() => signaler.signal('tick'), 60 * 1000);
}
}