ceckenrode
ceckenrode

Reputation: 4703

When to use dispatch with alt.js flux

I'm new to using flux and have started using the alt.js implmentation. I'm wondering when I would use dispatch from within my actions. For example, take this code.

//ImageActions.js

class ImageActions {
  getImages(id) {
    return Api.get(`topics/${id}`).then(response => {
      let images = response.data.filter(image => {
        return !image.is_album;
      });
      this.updateImages(images);
    });
  }
  updateImages(images) {
    return images;
  }
}
---------------------------------------------------

//ImageStore.js
class ImageStore {
  constructor() {
    this.images = [];
    this.image = {};
    this.bindListeners({
      handleUpdateImages: ImageActions.UPDATE_IMAGES
    });
  }
  handleUpdateImages(images) {
    this.images = images;
  }
}

Currently this works without using the dispatch() function as seen in their tutorial here http://alt.js.org/guide/async/

I'm wondering when I'd want to do this and what dispatch does and what it does differently than just returning the value from the updateImages function in ImageaActions.js

Upvotes: 1

Views: 456

Answers (1)

JorgeObregon
JorgeObregon

Reputation: 3310

You use dispatch when your async calls resolve. In this case it works because when your sync call finishes, you are calling another action (updateImages) which is triggering the dispatch, since getImages is not triggering a dispatch. Remember the return of an async call is a Promise.

Upvotes: 2

Related Questions