2015-03-27 20 views
6

'da bir gecikme olmadan çalışmayacaktır Reflü'yi kullanıyorum ve normalde ajax çağrısı yaptıktan sonra tetikliyorum ve iyi çalışıyor. Test amaçlı olarak ajax aramasına ihtiyacım yoktu ve 5 ms'lik bir zaman aşımı vermediğimde tetikleyicinin çalışmayacağını fark ettim. Çalışıyor ve çalışmıyor örnek. Reflux tetikleyici, init

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! 

Bu çalışacaktır:

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) 

sen neden bir gecikme olmadan çalışmaz açıklamak ve gerektiği Can

örneği Çalışmıyor? Bu bir hata mı, anlamadığım bir şey mi?

cevap

5

Bunun nedeni, bileşenlerin boş diziyi getInitialState'dan alması ve trigger çağrıldıktan sonra gerçekleşmesidir. Mağaza örneği oluşturulduğunda, yani bir bileşen yüklenmeden önce fetchThreads'daki tetikleyicinin hemen çağrılması anlamına gelençağrılır. Dinleme Bileşeni daha sonra monte edildiğinde, boş diziyi getInitialState yerine mağazadan alır.

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

Aşağıdaki değişiklik öneririm