Search code examples
reactjsrefluxjs

Reflux listenAndPromise do action many times


i have some actions in reflux actions, this is my actions

var BeritaActions = Reflux.createActions({
  'getListBerita': {
    children: [
      'actions', 'completed', 'failed'
    ]
  },
  'getBerita': {
    children: [
      'actions', 'completed', 'failed'
    ]
  },
});

BeritaActions.getListBerita.listen(function(param)
{
  return BeritaUtil.listBerita(param)
    .on('error', this.failed)
    .end(this.completed);
});

BeritaActions.getBerita.listenAndPromise(function(id)
{
  return BeritaUtil.read(id)
    .on('error', this.failed)
    .end(this.completed);
});

this is my store, and listen to actions

Reflux.createStore({
  onprogress: false,
  type: null,
  init()
  {
    this.listenTo(BeritaAct.getListBerita.completed, this.getInitData);
    this.listenTo(BeritaAct.getListBerita.failed, this.getInitErrorData);
  },
  setType(type)
  {
    return this.type = type;
  },
  getCurrentData()
  {
    return _data;
  },
  getInitData(field)
  {
    console.log(field)
    let data = JSON.parse(field.text);
    if(data.meta.code == 200)
    {
      if(typeof _data[this.type] == 'undefined')//first open
      {
        //console.log('first')
        _data[this.type] = data.data;
      }else//on load more = merging data
      {
        //console.log(_data[this.type])
        _data[this.type] = update(_data[this.type], {$merge: data.data}); 
      }

      this.trigger(_data);

    }else
    {
      Toas.error({title:data.meta.message, content:''});
    }
  },...

so i execute actions in my components

React.createClass({
  getInitialState()
  {
    if(Progress.isAjax())
    {
      Progress.onProgress(true);
      BeritaStore.setType('list');
      BeritaAct.getListBerita({});
    }else
    {
      //not ajax
    }
    return {
      showloader: {display: 'none'},
      shownext: {display: 'block'}
    };
  },..

store can listen actions so well and can return on my react component. but when i check network inspect, i got the action request many time, i don't know what happening ?

request many times


Solution

  • ok guys i solved that problem, let going to getInitData function

    getInitData(bind, field)
    {
        console.log(field)
    }
    

    i add parameter on getInitData function, so after i console.log() second paramater, i get the data