- Architecting Angular Applications with Redux,RxJS,and NgRx
- Christoffer Noring
- 138字
- 2021-08-27 19:56:19
Adding the store
The store will act as the data source for our state but will also be able tell any listeners when a change to the store happens:
// demo/store.js
import dispatcher from './dispatcher';
function selectIndex(index) {
store["selectedIndex"] = index;
}
// 1) store registers with dispatcher
dispatcher.register(message => {
switch (message.type) {
// 3) message is sent by dispatcher ( that originated from the first view)
case "SELECT_INDEX":
selectIndex(message.data);
// 4) listener, a view, is being notified of the change
store.emitChange();
break;
}
});
class Store {
constructor() {
this.listeners = [];
}
// 2) listener is added by a view
addListener(listener) {
if (!this.listeners["change"]) {
this.listeners["change"] = [];
}
this.listeners["change"].push(listener);
}
emitChange() {
if (this.listeners["change"]) {
this.listeners["change"].forEach(cb => cb());
}
}
getSelectedItem() {
return store["selectedIndex"];
}
}
const store = new Store();
export default store;
推薦閱讀
- EDA技術(shù)與VHDL編程
- Twilio Cookbook
- Hands-On Industrial Internet of Things
- Django 2 by Example
- HTML5 Game development with ImpactJS
- 通信簡史:從信鴿到6G+
- Practical Web Design
- 電力物聯(lián)網(wǎng)工程技術(shù)原理與應(yīng)用
- 網(wǎng)管員必讀:網(wǎng)絡(luò)管理(第2版)
- 2小時讀懂物聯(lián)網(wǎng)
- 新手易學(xué):新手學(xué)淘寶開店
- React Cookbook
- 局域網(wǎng)組成實踐
- 5G非正交多址接入技術(shù):理論、算法與實現(xiàn)
- 精通SEO:100%網(wǎng)站流量提升密碼