Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejs
// Subscription to the event to get favorites
window.addEventListener('message', (event) => {
  if (event.data && event.data.eventName === 'getFavorites') {
    // Event handling
  }
}, false);

Data format:
event.data = {
  eventName: 'getFavorites',
  payload: { 
    list: ['vendor_code_1', 'vendor_code_2'],
    },
  },
}

Simple example:

Code Block
languagejs
<script>
  const items = [];
  
  window.addEventListener('message', (event) => {
      if (event.data && event.data.eventName === 'iframeLoaded') {
          window.frames.wizartFittingRoom.postMessage({
              eventName: 'setFavoritesStore',
              payload: items,
          }, '*');
      } else if (event.data && event.data.eventName === 'addFavorite') {
          items.push(event.data.payload.item.vendor_code);
          window.frames.wizartFittingRoom.postMessage({
              eventName: 'setFavoritesStore',
              payload: items,
          }, '*');
      } else if (event.data && event.data.eventName === 'removeFavorite') {
          const foundIndex = items.findIndex(
              (item) => item === event.data.payload.item.vendor_code,
          );
          if (foundIndex !== -1) {
              items.splice(foundIndex, 1);
              window.frames.wizartFittingRoom.postMessage({
                      eventName: 'setFavoritesStore',
                      payload: items,
                  },
                  '*',
              );
          }
      }
  }, false);
</script>