morlay
8/19/2015 - 11:41 AM

ListStore.js

import { createStore } from 'reflux';

import GalleryActions from '../actions/GalleryActions'

export default createStore({

  init(){
    this.galleryMap = {}
    this.listenTo(GalleryActions.listGalleries.completed, 'onGalleryListUpdate')
    this.listenTo(GalleryActions.getGallery.completed, 'onGalleryUpdate')
  },

  onGalleryListUpdate(galleryList){
    galleryList.forEach(this.setGallery, this)
    this.trigger()
  },

  onGalleryUpdate(gallery){
    this.setGallery(gallery)
    this.trigger()
  },

  setGallery(gallery){
    if (gallery.id) {
      this.galleryMap[gallery.id] = gallery
    }
  },

  getGalleryById(galleryId){
    return this.galleryMap[galleryId]
  }
})