简体   繁体   English

回流商店处于一种不听动作的情况

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

I have a Reflux store that is listening to a 'customerLoaded' action. 我有一家Reflux商店正在监听“ customerLoaded”操作。 This action supplies customer details to the store. 此操作将客户详细信息提供给商店。 When the store received the new or updated customer info, it is triggered to notifiy listening components of new or updated customer details. 当商店收到新的或更新的客户信息时,将触发该通知以通知侦听新的或更新的客户详细信息的组件。

(all code has been translated from Dutch into English, so there might be a typo or two) (所有代码都已从荷兰语翻译成英语,因此可能会有一两个错字)

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;
}
});

And I have my Reflux actions. 我有我的反流动作。

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: ...
      });
      }
    };

The problem occurs when the setEmail action is being used. 使用setEmail操作时,会发生此问题。 In fact this action works like it should and the email address is updated on the server. 实际上,此操作按应有的方式工作,并且电子邮件地址已在服务器上更新。 Even the action to reload the customer details from the server is triggered, but in the loadCustomer.preEmit, nothing happens when the 'CustomerActions.customerLoaded(data)' action is triggered. 即使触发了从服务器重新加载客户详细信息的操作,但在loadCustomer.preEmit中,触发“ CustomerActions.customerLoaded(data)”操作时也没有任何反应。 The CustomerStore does not respond to this action. CustomerStore不响应此操作。 The 'data' parameter contains data as expected, so that's not the problem. 'data'参数包含预期的数据,所以这不是问题。

The strangest thing is that to initially display customer details on the screen, the same combination of CustomerActions.loadCustomer and the CustomerStore is used without any problems. 最奇怪的是,要最初在屏幕上显示客户详细信息,可以使用CustomerActions.loadCustomer和CustomerStore的相同组合,而不会出现任何问题。 This combination also still works when I use it to display details of a different customer. 当我使用它来显示其他客户的详细信息时,该组合也仍然有效。

Does anybody have a clue on what might be the problem, or what I can do to check why the store does not respond to the action? 是否有人知道可能是什么问题,或者我能做些什么来检查商店为什么不响应该行为?

Thanks in advance! 提前致谢!

Try to rewrite you code using example async workflow instead of using preEmit hook. 尝试使用示例异步工作流而不是使用preEmit钩子重写代码。 If you need validation i think you should use shouldEmit hook instead of preEmit. 如果您需要验证,我认为您应该使用shouldEmit钩子而不是preEmit。 But first try without hooks at all. 但是,首先尝试不使用任何钩子。 https://github.com/spoike/refluxjs#asynchronous-actions https://github.com/spoike/refluxjs#asynchronous-actions

Maybe this will help. 也许这会有所帮助。

The problem has been solved by 'requiring' the store that should handle te action from the JS file with the view that initiates the change of the email address (calls the CustomerActions.setEmail action). 通过“要求”应处理JS文件中的te操作的商店,并通过该视图启动更改电子邮件地址(调用CustomerActions.setEmail操作),解决了该问题。 This solution does not correspond with my expectations. 该解决方案与我的期望不符。 I expected that, because the store already is being used (and is working), it already is in memory and therefore ought to be responding to actions that a listener is registered for. 我希望,因为该存储库已经在使用中(并且正在运行),所以它已经在内存中,因此应该响应注册了侦听器的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM