tl;dr : GraphStore の UUID は、新しいグラフを追加するたびに変更されます。これにより、各グラフが独自の独自の GraphStore を作成していると想定できます。1店舗をシェアしてほしい。
複数のグラフ コンポーネントを含む React ダッシュボード コンポーネントがあります。
マイ グラフ コンポーネントには、ダッシュボードから id props が渡されます。その ID を使用して、GraphStore に格納されているグラフ配列内のデータを探します。ただし、すべてのグラフが同じ (望ましい動作) を共有するのではなく、各グラフが独自の GraphStore を作成しているように思えます。それらすべてが同じ GraphStore を使用するようにするにはどうすればよいですか?
ダッシュボードから正しい GraphStore を渡すことを考えましたが、各グラフに GraphStore からの変更をリッスンさせることはできません。
私はReflux.connectFilterを使わないことに満足していますが、これには完璧なもののようです.
私のコード(少なくとも重要な部分):
ダッシュボード
var React = require('react');
var Graph = require('./graph').Graph;
var GraphActions = require('./graphActions').GraphActions;
var UUID = require('uuid');
var Dashboard = React.createClass({
...
render: function() {
var graphs = [];
for(var i = 0; i < 10; ++i) {
var id = UUID.v4();
GraphActions.createGraph(id);
graphs.push(
<Graph id={id} />
);
}
}
});
module.exports = {Dashboard: Dashboard};
グラフ
var React = require('react');
var GraphStore = require('./graphStore').GraphStore;
var Graph = React.createClass({
mixins: [Reflux.connectFilter(GraphStore, "graph", function(){
return graphs.filter(function(graph) {
return graph.id === this.props.id;
}.bind(this))[0];
})],
propTypes: {
id: React.PropTypes.string
},
render: function() {
// Needed because the AJAX call in GraphStore might not have completed yet
if(typeof this.state.graph == "undefined") {
return (<div>Graph loading...</div>);
}
return (<div>Data: {this.state.graph.data}</div>);
}
});
module.exports = {Graph: Graph};
グラフストア
var Reflux = require('reflux');
var jQuery = require('jquery');
var GraphActions = require('./graphActions').GraphActions;
var UUID = require('uuid');
var GraphStore = Reflux.createStore({
listenables: [GraphActions],
onCreateGraph: function(graphId) {
console.log("GraphStore " + this.id + " is adding new graph " + graphId);
jQuery.ajax({
...
success: this.addGraph
});
},
addGraph: function(data) {
this.graphs.push(
{
id: graphId,
data: data
}
);
this.trigger({graphs: this.graphs});
},
getInitialState: function() {
this.graphs = [];
// Here I give the store a UUID so I can identify it later
this.id = UUID.v4();
return {
graphs: this.graphs
};
}
});