Search code examples
javascriptjqueryarraysjsongalleria

Request server side JSON data and pass into Galleria 1.5.7 framework


How to pass JSON object asynchronously retrieved from server using $getJSON callback ( or some other recommended method ) and pass this into the Galleria 1.5.7 dataSource variable as a custom JSON data array ? Online docs shows how to feed a manually created custom data array stored in a JS config file, all working fine - but no docs how to retrieve JSON objects from server.

https://docs.galleria.io/references/data.html

https://docs.galleria.io/options/dataSource.html

Sample lint-validated JSON server data: data.json

{ "title": "title of art work 1", "description": "Description of art work 1", "image": "../../../../images/image1_fullscreen.png" }

Seeing only a blank stage.

Thanks - working fine now using the lower level jQuery ajax utility function suggested. The server side JSON data also needed a little tweak to encapsulate it with square brackets so that the Galleria framework recognised it correctly for parsing as an array of objects.

[ { "title": "title of art work 1", "description": "Description of art work 1", "image": "../../../../images/image1_fullscreen.png" } ]


Solution

  • According to https://docs.galleria.io/options/dataSource.html you can use a custom data array, however you need the data before running the Galleria framework.

    1. Fetch the data from your server
    2. Use that data to run the Galleria framework
    $.ajax({
      url: 'data.json',
      success: function(data) {
        Galleria.run('.galleria', {
          dataSource: data
        });
      }
    });
    

    Or in your case with getJSON:

    $.getJSON('data.json', function(data) {
        Galleria.run('.galleria', {
          dataSource: data
        });
    });