【问题标题】:fluxxor add more than 1 set of actions to a flux instanceFluxxor 向一个 Flux 实例添加多于 1 组的动作
【发布时间】:2015-06-15 14:27:40
【问题描述】:

我已经使用 Fluxxor 设置了一个 React 应用程序,我正在尝试使用一个控制器视图,它可以获取多个存储和操作,类似于 carousel example

但是,在fluxxor 示例中,它使用了多个商店,但只使用了一组操作。我想知道如何传递多个动作。

var React = require('react/addons'),
    Fluxxor = require("fluxxor"),
    authorActions = require("../actions/author-actions"),
    authorStore = require("../stores/author-store"),
    productActions = require("../actions/product-actions"),
    productStore = require("../stores/product-store");

var stores = { ProductStore: new productStore(), AuthorStore: new authorStore()  };
// how do I combine my actions here?
var flux = new Fluxxor.Flux(stores, actions);

【问题讨论】:

    标签: javascript reactjs flux


    【解决方案1】:

    添加到@BinaryMuse 答案。我喜欢为我的操作命名,这样就不会在任何通用命名的操作上发生冲突。

    只需使用每个命名空间的键启动一个对象:

    ...
    var actions = { author: authorActions, product: productActions  };
    var flux = new Fluxxor.Flux(stores, actions);
    

    然后用它们的命名空间调用它们:

    this.getFlux().actions.author.add("Aldus", "Huxley", ...);
    

    【讨论】:

      【解决方案2】:

      助焊剂supports adding actions dynamically:

      var flux = new Fluxxor.Flux(stores);
      flux.addActions(authorActions);
      flux.addActions(productActions);
      

      如果命名空间不冲突,您还可以将它们与Underscore's extend 之类的东西合并在一起:

      var actions = _.extend({}, authorActions, productActions);
      

      Object.assign(或polyfill):

      var actions = Object.assign({}, authorActions, productActions);
      

      【讨论】:

        猜你喜欢
        • 2014-05-18
        • 2015-05-24
        • 1970-01-01
        • 1970-01-01
        • 2016-02-02
        • 2014-03-16
        • 1970-01-01
        • 2017-12-23
        • 2015-12-14
        相关资源
        最近更新 更多