我一直在寻找一整天,通过从我的产品列表(this.props.products)中选择一个新项目来更新我的 ShoppingCart(this.props.shoppingCart)。
我找到了一种方法,但它似乎真的很麻烦和复杂,那就是使用这样的调度程序:
如果我没记错的话,这也需要 npm-package flex 和 node.js ?这并不理想,因为我使用的是 Visual Studio 和 ReactJS.NET ..
那么......我到底怎么能以容易管理的方式做到这一点?
全球调度员:
var ShoppingCartStore = {
shoppingCart: [],
dispatcher: new Dispatcher(),
addToCart: function (item) {
this.shoppingCart.push(item);
this.dispatcher.dispatch({
actionType: 'updated',
data: this.shoppingCart
});
},
};
用法 :
addToCart: function (e) {
var shoppingCartItem = {
name: this.props.productData.name,
price: this.props.productData.price,
description: this.props.productData.description
};
ShoppingCartStore.dispatcher.subscribe(function () {
});
ShoppingCartStore.addToCart(shoppingCartItem);
},
如何更新?
var ShoppingCartBig = React.createClass({
componentDidMount: function () {
ShoppingCartStore.dispatcher.subscribe(function (o) {
this.setState({ shoppingCart: o.data });
});
},