【发布时间】:2016-03-31 20:04:10
【问题描述】:
我目前正在尝试 FLUX 设计模式,但偶然发现了一个问题。在这个项目中,我也使用 ReactJS 来配合它。它工作正常,几乎完成了,但搞砸了。因此,我使用我呈现的按钮启动函数,单击它会触发函数链。
render: function(){
return (
<div><button onClick={this.waiting}>Hello</button>
{this.state.results}
</div>
)
}
您现在可以看到,当单击此按钮时,它会触发一个名为 waiting() 的函数,其中包含以下内容
waiting: function() {
actions.trending();
return this.state.results;
},
所以它会触发函数并发生以下情况
var actions = {
trending: function(){
api.trending()
.then(function(result){
dispatcher.dispatch({
actionType: actionConstants.TRENDING_RESULT,
results: result
});
}, function(error){
console.log(error);
dispatcher.dispatch({
actionType: actionConstants.ERROR,
error: error
});
});
}
};
一切正常,我正在获取我的数据,到目前为止我很高兴,问题是接下来会发生什么,当调度程序将 actionType 与数据一起发送时,它会进入我拥有的商店。然后在我的存储文件中注册有效负载(操作)。
dispatcher.register(function(action){
switch (action.actionType) {
case actionConstants.TRENDING_RESULT:
console.log(action.results); <- I can actually see my data
results = action.results;
resultErrors = null;
SearchStore.emit(actionConstants.TRENDING_RESULT); <- Error
break;
case actionConstants.ERROR:
results = null;
resultErrors = action.error;
console.log(resultErrors);
SearchStore.emit(actionsConstants.ERROR);
break;
}
});
因此,此时我可以在 console.log 中看到我的数据,但在发出如下声音的函数中出现错误
Uncaught (in promise) TypeError: this._events[evt].push
对于我的商店功能,我使用以下内容
var SearchStore = {
getTrending: function() {
return JSON.stringify(results);
},
getError: function() {
return resultErrors;
},
emit: function(event) {
eventEmitter.on(event);
},
on: function(event, callback) {
eventEmitter.on(event, callback);
},
removeListener: function(event, callback) {
eventEmitter.removeListener(event, callback);
}
};
最后为了接收任何发射,我在 ComponentDidMount 中调用我的 on 函数,看起来像这样
componentDidMount: function(){
SearchStore.on(actionConstants.TRENDING_RESULT, this.loadResults());
SearchStore.on(actionConstants.ERROR, this.showError());
},
componentWillUnmount: function(){
SearchStore.removeListener(actionConstants.TRENDING_RESULT, this.loadResults());
SearchStore.removeListener(actionConstants.ERROR, this.showError());
},
对于调度程序,我使用 Facebook 的 FLUX 调度程序,对于发射器,我使用 eventemitter3。一切都很顺利,直到我尝试发出 TRENDING_RESULT 和有效载荷。对于这个问题的篇幅,我感到非常抱歉,但我想尽可能详尽,以便您理解。
【问题讨论】:
标签: node.js reactjs flux eventemitter