簡體   English   中英

回流商店處於一種不聽動作的情況

[英]Reflux store is in one situation not listening to an action

我有一家Reflux商店正在監聽“ customerLoaded”操作。 此操作將客戶詳細信息提供給商店。 當商店收到新的或更新的客戶信息時,將觸發該通知以通知偵聽新的或更新的客戶詳細信息的組件。

(所有代碼都已從荷蘭語翻譯成英語,因此可能會有一兩個錯字)

var CustomerStore = Reflux.createStore({
init: function ()
{
    this.listenTo(CustomerActions.customerLoaded, this._onCustomerLoaded);
},
// handlers
_onCustomerLoaded: function (data)
{
    if (data)
    {
        _customers[data.Number] = data;
        this.trigger(data.Number);
    }
},
getCustomer: function(number)
{
  var customer = null;
  if(_customers[number])
  {
    customer = _customers[number];
  }
  else
  {
    CustomerActions.loadCustomer(number);
  }
  return customer;
}
});

我有我的反流動作。

var CustomerActions = Reflux.createActions(
[
  "loadCustomer",
  "customerLoaded",
  "setEmail",
]
);

CustomerActions.loadCustomer.preEmit = function (number)
{
    if (!number|| number=== 0 || number=== '')
    {
        CustomerActions.customerLoaded(null);
    }
    else
    {
      // request details from server
      CustomerApi.getCustomerByNumber(number)
        .done(function (data)
          {
              // send details to the store
              CustomerActions.customerLoaded(data);
           }
        );
    }
};

CustomerActions.setEmail.preEmit = function (number, newValue)
{
  if(number && number !== 0 && number !== '')
  {
    // send updated email address to server
    CustomerApi.setEmail(number, newValue)
      .done(function (result)
      {
        if(result)
        {
          // start action to load customer details from the server
          CustomerActions.loadCustomer(number);
        }
        // else.. TODO: ...
      });
      }
    };

使用setEmail操作時,會發生此問題。 實際上,此操作按應有的方式工作,並且電子郵件地址已在服務器上更新。 即使觸發了從服務器重新加載客戶詳細信息的操作,但在loadCustomer.preEmit中,觸發“ CustomerActions.customerLoaded(data)”操作時也沒有任何反應。 CustomerStore不響應此操作。 'data'參數包含預期的數據,所以這不是問題。

最奇怪的是,要最初在屏幕上顯示客戶詳細信息,可以使用CustomerActions.loadCustomer和CustomerStore的相同組合,而不會出現任何問題。 當我使用它來顯示其他客戶的詳細信息時,該組合也仍然有效。

是否有人知道可能是什么問題,或者我能做些什么來檢查商店為什么不響應該行為?

提前致謝!

嘗試使用示例異步工作流而不是使用preEmit鈎子重寫代碼。 如果您需要驗證,我認為您應該使用shouldEmit鈎子而不是preEmit。 但是,首先嘗試不使用任何鈎子。 https://github.com/spoike/refluxjs#asynchronous-actions

也許這會有所幫助。

通過“要求”應處理JS文件中的te操作的商店,並通過該視圖啟動更改電子郵件地址(調用CustomerActions.setEmail操作),解決了該問題。 該解決方案與我的期望不符。 我希望,因為該存儲庫已經在使用中(並且正在運行),所以它已經在內存中,因此應該響應注冊了偵聽器的操作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM