简体   繁体   中英

Reflux store is in one situation not listening to an action

I have a Reflux store that is listening to a 'customerLoaded' action. 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. 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. The CustomerStore does not respond to this action. The 'data' parameter contains data as expected, so that's not the problem.

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. 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. If you need validation i think you should use shouldEmit hook instead of preEmit. But first try without hooks at all. 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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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