Here's a pretty fundamental JavaScript and Aurelia question.
Let's say that I have a singleton object, for example User
and it would often get updates from the server, which returns a whole new User
object.
Now, to push the update to the views, I have two options (that I know of):
Update every property of the existing
User
to that of the newUser
's manually (this would also require mapping every property).Replace the object reference and push an
EventAggregator
notification for all listeners to re-query theUser
object.
I have currently gone for option number 1, which has raised some issues, but nothing blocking.
Which one would be more efficient and/or provide more benefits over the other?
Here's my opinion. You don't have to use
EventAggregator
, and you also don't have to struggle updating every property. You could create helper class (AppState
or something) to hold yourUser
object. In your elements, inject theAppState
class and create a getter function to return theUser
object (use@computedFrom
oraurelia-computed
to avoid dirty-checking). For example:JS
HTML
Running example https://gist.run/?id=f2ed9769343513b0819115863ff64c34