I have a simple class, Event
with a computed property:
import moment from 'moment';
export class Event {
constructor(data) {
Object.assign(this, data);
}
get playedFromNow() {
return moment(this.CreateDate).fromNow();
}
}
playedFromNow
just returns a string based on the CreateDate
property, like 7 minutes ago
.
The viewmodel gets an array of events and the view renders the events. The array gets updated via websockets every time a new event occurs (every few minutes).
<div repeat.for="event of events">
<div class="media-body">
<h4 class="media-heading">${event.title} <small>${event.playedFromNow}</small></h4>
</div>
</div>
And the (relevant) viewmodel code:
let socket = io();
socket.on(`new-event`, (data) => {
this.events.unshift(new Event(data)); // add to the event array at the top
});
// subscribe
let subscription = bindingEngine.collectionObserver(this.events).subscribe();
// unsubscribe
subscription.dispose();
Currently the property is dirty checked, which means the property is checked and changes very frequently - this is a bit unnecessary and there are a lot of events shown on a screen so I'm concerned about performance over time. Is there a way that I can trigger the recalculation based on the array binding and update code in the VM?: