fluxxor向一个flux实例添加一组以上的操作

fluxxor add more than 1 set of actions to a flux instance

本文关键字:一组 操作 flux 一个 fluxxor 实例 添加      更新时间:2023-09-26

我已经使用Fluxxor设置了一个React应用程序,我正在尝试使用一个可以获得多个存储和操作的控制器视图,类似于这个旋转木马示例。

然而,在fluxor示例中,它使用了多个存储,但只使用了一组操作。我想知道如何通过多个动作。

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

Fluxxor支持动态添加操作:

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

如果名称空间没有冲突,您也可以使用Underscore的extend:之类的东西将它们合并在一起

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

Object.assign(或polyfill):

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

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

只需为每个名称空间初始化一个带有密钥的对象:

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

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

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