我使用反流,通常是在发出ajax调用之后触发,并且运行良好。出于测试目的,我不需要ajax调用,并且我注意到,除非我给出至少5ms的超时时间,否则触发器不会工作。这里是工作的例子,而不是工作的例子。
不起作用的例子:
window.threadStore = Reflux.createStore
init: ->
@state = @getInitialState()
@fetchThreads()
getInitialState: ->
loaded: false
threads: []
fetchThreads: ->
# ajax call for not Testing, and just trigger for Testing
@state.threads = FakeData.threads(20)
@state.loaded = true
@trigger(@state) # This will NOT work!
这将起作用:
window.threadStore = Reflux.createStore
init: ->
@state = @getInitialState()
@fetchThreads()
getInitialState: ->
loaded: false
threads: []
fetchThreads: ->
# ajax call for not Testing, and just trigger for Testing
@state.threads = FakeData.threads(20)
@state.loaded = true
setTimeout( =>
@trigger(@state) # This WILL work!
, 500)
你能解释一下为什么它不能立即工作吗?应该吗?是窃听器还是什么我不明白的东西。
发布于 2015-04-01 06:41:24
这是因为组件从getInitialState获取空数组,并且在调用trigger之后发生。
在创建存储实例时调用init,这意味着在安装组件之前立即调用fetchThreads中的触发器。稍后,当侦听组件被挂载时,它将从getInitialState上的存储中获取空数组。
我建议作以下修改:
window.threadStore = Reflux.createStore
init: ->
@state =
loaded: false
threads: []
@fetchThreads()
getInitialState: ->
@state # TODO: State should be cloned for sake of concurrency
fetchThreads: ->
# NOTE: Assign a new state for the sake of concurrency
@state =
loaded: true
threads: FakeData.threads(20)
@trigger(@state) # This will SHOULD work now ;-)https://stackoverflow.com/questions/29308672
复制相似问题