To tell the view that something has happened and act on it, three things need to happen:
- The view needs to register with the store as a listener
- The store needs to send off an event conveying that a change has happened
- The view needs to reload its data
Starting with the store, we need to build it out so that you can register as a listener to its events. We therefore add the addListener() method:
// store-with-pubsub.jsfunction selectIndex(index) { store["selectedIndex"] = index;}// registering with the dispatcherdispatcher.register(message => { switch (message.type) { case "SELECT_INDEX": selectIndex(message.data); // signals to the listener that a change has happened store.emitChange(); break; }});class Store { constructor() ...