<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 七、Store Store 保存整個應用的狀態。它的角色有點像 MVC 架構之中的Model 。 在我們的 Demo 中,有一個[`ListStore`](https://github.com/ruanyf/extremely-simple-flux-demo/blob/master/stores/ListStore.js),所有數據都存放在那里。 > ~~~ > // stores/ListStore.js > var ListStore = { > items: [], > > getAll: function() { > return this.items; > }, > > addNewItemHandler: function (text) { > this.items.push(text); > }, > > emitChange: function () { > this.emit('change'); > } > }; > > module.exports = ListStore; > ~~~ 上面代碼中,`ListStore.items`用來保存條目,`ListStore.getAll()`用來讀取所有條目,`ListStore.emitChange()`用來發出一個"change"事件。 由于 Store 需要在變動后向 View 發送"change"事件,因此它必須實現事件接口。 > ~~~ > // stores/ListStore.js > var EventEmitter = require('events').EventEmitter; > var assign = require('object-assign'); > > var ListStore = assign({}, EventEmitter.prototype, { > items: [], > > getAll: function () { > return this.items; > }, > > addNewItemHandler: function (text) { > this.items.push(text); > }, > > emitChange: function () { > this.emit('change'); > }, > > addChangeListener: function(callback) { > this.on('change', callback); > }, > > removeChangeListener: function(callback) { > this.removeListener('change', callback); > } > }); > ~~~ 上面代碼中,`ListStore`繼承了`EventEmitter.prototype`,因此就能使用`ListStore.on()`和`ListStore.emit()`,來監聽和觸發事件了。 Store 更新后(`this.addNewItemHandler()`)發出事件(`this.emitChange()`),表明狀態已經改變。 View 監聽到這個事件,就可以查詢新的狀態,更新頁面了。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看