React Flux存储器发出的事件不起作用

时间:2015-08-02 05:32:19

标签: javascript reactjs reactjs-flux flux

由于某种原因,EventEmitter的发射事件无法正常工作 HomeStore.emit(CHANGE_EVENT)
这是因为我使用Coffeescript吗?

我尝试将5篇文章作为initialState获取到数组中。 当商店发出更改事件时,Component中的_onChange方法应调用HomeStore.newsLatest()来更新列表。

Home.js.jsx.coffee

  HomeActions = require "../actions/HomeActions.js.coffee"
  HomeStore = require "../stores/HomeStore.js.coffee"

  Latest = React.createClass {

    getInitialState: ->

      {
        list_latest: HomeActions.fetchLatest(1)
      }
    componentWillMount: ->
      HomeStore.addChangeListener(this._onChange)
    componentDidMount: ->

    componentWillUnmount: ->
      HomeStore.removeChangeListener(this._onChange)
    _onChange: ->
      this.setState {list_latest: HomeStore.newsLatest()}
      console.log "update list_latest"
    render: ->
      `<article className="post-article article clearfix">
      <h2 className="title awesome">
          adfsda
      </h2>
      </article>
      `

  }

HomeActions.js.coffee

HomeDispatcher = require "../dispatchers/HomeDispatcher.js.coffee"
HomeConstants = require "../constants/HomeConstants.js.coffee"

HomeActions =
  fetchLatest: (page) ->
    HomeDispatcher.handleAction {
      actionType: HomeConstants.FETCH_LATEST,
      page: page
    }
  fetchPopular: ->
    HomeDispatcher.handleAction {
      actionType: HomeConstants.FETCH_POPULAR
    }
  fetchFacebook: ->
    HomeDispatcher.handleAction {
      actionType: HomeConstants.FETCH_FACEBOOK
    }

module.exports = HomeActions

HomeStore.js.coffee

HomeDispatcher = require('../dispatchers/HomeDispatcher.js.coffee')
HomeConstants = require('../constants/HomeConstants.js.coffee')
objectAssign = require('object-assign')
EventEmitter = require('events').EventEmitter
CHANGE_EVENT = 'change'

_newsLatest =

  list: []
  set: (data) ->
    console.log "set #{data}"
    @list = data

_newsPopular =
  list: []

_newsFacebook =
  list: []

HomeStore = objectAssign({}, EventEmitter.prototype,
  addChangeListener: (callback) ->
    this.on(CHANGE_EVENT, callback)
  removeChangeListener: (callback) ->
    this.removeListener(CHANGE_EVENT, callback)
  newsLatest: ->
    _newsLatest.list
  setData: (d) ->
    _newsLatest.set d
  newsPopular: ->
    _newsPopular.list
  newsFacebook: ->
    _newsFacebook.list
)

HomeDispatcher.register (payload) ->
  action = payload.action
  switch action.actionType
    when HomeConstants.FETCH_LATEST
      console.log 'FETCH_LATEST'

      $.ajax
        url: HomeConstants.API_URL + action.page
        dataType: "json"
        method: "get"
        success: (res) ->
          HomeStore.setData res.articles

      HomeStore.emit(CHANGE_EVENT)
    when HomeConstants.FETCH_POPULAR
      console.log 'FETCH_POPULAR'
      return HomeStore.emit(CHANGE_EVENT)
    when HomeConstants.FETCH_FACEBOOK
      console.log 'FETCH_FACEBOOK'
      return HomeStore.emit(CHANGE_EVENT)
    else
      true

module.exports = HomeStore

1 个答案:

答案 0 :(得分:0)

我认为你并没有在合适的时间附加你的事件监听器。

componentWillMount: ->
  HomeStore.addChangeListener(this._onChange)

这会尝试在装入组件之前向组件添加更改侦听器。尝试将其添加到componentDidMount

componentDidMount: ->
  HomeStore.addChangeListener(this._onChange)

编辑(反映评论):

switch action.actionType
  when HomeConstants.FETCH_LATEST
    console.log 'FETCH_LATEST'

    $.ajax
      url: HomeConstants.API_URL + action.page
      dataType: "json"
      method: "get"
      success: (res) ->
        HomeStore.setData res.articles
        HomeStore.emit(CHANGE_EVENT)
    return