mercredi 27 mai 2020

Checking for updates on an object and custom events

I've got a stupid question that's stumped me a little at the moment. Feel like I'm missing something obvious. For the purpose of this I have 3 different javascript objects.

The model, which details a device and its attributes.

The display, which reads data from the model and loads it to a webpage display.

And finally, the Updater which updates the model's attributes.

Model is called and read by the display, while it is called and written to from the updater.

What I'm trying to do is to reflect live updates from the updater. The updater is a message handler for a websocket connection to get live data. I need the display to update to reflect the newest data in the model.

I have attempted doing this with event listeners but seem to hit a wall with their assignment.

If the event is part of the model event = new Event('name') or event = new CustomEvent('name'), it errors in compilation that it is an invalid type. There is no error if it is simply event = Event('name'), but that then errors in the app, stating that 'new' needs to be there. With that implementation, I attached an event listener to the model in the display:

model.addEventListener('name', () => {
    // update info
});

And then I try to fire the event in the updater (message handler), so that after the model's attributes are updated, it will fire the event so the display will catch it.

Obviously I am hitting issues with attaching the event to the model object. Am I missing something with syntax and/or best practice as to how I should attach the event, listener and how to fire it or should I be doing something completely different?

Any help would be appreciated, thanks!




Aucun commentaire:

Enregistrer un commentaire