question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Append content dynamic with Json

See original GitHub issue

Hi,

I’m trying to append content dynamically from a Json file but I cannot find a work around to make it work.

import Glide from '@glidejs/glide';

function slider() {
  let ul = document.querySelector('.glide__slides');
  let card = '';
  var glide = new Glide('.glide').destroy();

  const photo = import('../metadata/photos.json').then((module) => {
    const data = module.default;
    data.forEach((photo) => {
      console.log(photo);
      card += `<li class="glide__slide"><img src="${photo.thumbnailUrl}" alt="${photo.title}">${photo.id}</li>`;
    });
    ul.innerHTML = card;
  });

  glide.mount();
}

slider();

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
albanyacademycommented, Sep 7, 2020

Looks like youre inserting dynamic content through an event listener but mounting glide outside of it. You need to mount glide after each time, but its more complicated - on each change, destroy previous glide, insert content, then construct a new glide and then mount. You need to do that last part because destroy and mount aren’t opposites, once youve destroyed mount wont reinit event listeners etc.

On Mon, Sep 7, 2020, 12:09 PM Nicolas Deyros, notifications@github.com wrote:

  1. why are you destroying an unmounted Glide?
  2. you don’t need to construct Glide before you’ve inserted your dynamic content. Shouldn’t cause a problem but its unnecessary to construct as a variable, do stuff, then mount in your current example. Better safe than sorry.

import Glide from ‘@glidejs/glide’;

function slider() { let ul = document.querySelector(‘.glide__slides’); let card = ‘’;

const photo = import(‘…/metadata/photos.json’).then((module) => { const data = module.default; data.forEach((photo) => { card += <li class="glide__slide"><img src="${photo.thumbnailUrl}" alt="${photo.title}">${photo.id}</li>; }); ul.innerHTML = card; });

// you don’t really need to use Glide as a const unless you’re accessing it elsewhere (e.g. construct, do stuff, then mount) new Glide(‘.glide’, {options??}).mount(); }

slider();

see: https://codepen.io/wearingalampshade/pen/BaKmMWR

Thanks for your help. I try to replicate the code but for some reason I cannot make it work

let ul = document.querySelector(‘.glide__slides’); let card = ‘’;

var init = { method: ‘GET’, headers: { ‘Content-Type’: ‘application/json’ }, mode: ‘cors’, cache: ‘default’, }; const url = new Request(‘/metadata/folletos.json’, init);

const fetchUrl = fetch(url).then((data) => { return data.json(); });

const provincias = ‘/metadata/dropdown.json’;

const provinciasSelect = document.querySelector(‘#provinciasSelect’); let provinciasSelected = ‘’;

fetch(provincias) .then((response) => { return response.json(); }) .then((data) => { data.forEach(function (str) { provinciasSelected += <option value="${str.id}">${str.provincia}</option>; }); //OUTPUT JSON DROPDOWN LIST provinciasSelect.innerHTML = provinciasSelected; });

provinciasSelect.addEventListener(‘change’, (e) => { provinciasSelectValue = e.target.value; let tmp = Promise.resolve(fetchUrl); fetchUrl.then((datas) => { datas[provinciasSelectValue].forEach((data) => { console.log(data); card += <li class="glide__slide">${data.title}</li>; }); ul.innerHTML = card; }); });

new Glide(‘.glide’).mount();

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/glidejs/glide/issues/515#issuecomment-688478756, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABFW3OSD2UQLWHICEAH2LGDSEUVWBANCNFSM4QL5UMLQ .

0reactions
nicolas-deyroscommented, Sep 7, 2020

I thinks I resolve the issue thank to you @albanyacademy doing this:

let ul = document.querySelector('.glide__slides');
let glide = document.querySelector('.glide');

var init = {
  method: 'GET',
  headers: { 'Content-Type': 'application/json' },
  mode: 'cors',
  cache: 'default',
};
const url = new Request('/metadata/folletos.json', init);

const fetchUrl = fetch(url).then((data) => {
  return data.json();
});

const provincias = '/metadata/dropdown.json';

const provinciasSelect = document.querySelector('#provinciasSelect');
let provinciasSelected = '';

fetch(provincias)
  .then((response) => {
    return response.json();
  })
  .then((data) => {
    data.forEach(function (str) {
      provinciasSelected += `<option value="${str.id}">${str.provincia}</option>`;
    });
    //OUTPUT JSON DROPDOWN LIST
    provinciasSelect.innerHTML = provinciasSelected;
    new Glide('.glide').destroy();
  });

provinciasSelect.addEventListener('change', (e) => {
  provinciasSelectValue = e.target.value;
  let card = '';
  fetchUrl.then((datas) => {
    datas[provinciasSelectValue].forEach((data) => {
      card += `<li class="glide__slide">${data.title}</li>`;
    });
    ul.innerHTML = card;
    new Glide('.glide', {
      type: 'carousel',
      startAt: 0,
      perView: 3,
    }).mount();
  });
});

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to add data dynamically in JSON object - Stack Overflow
Easy: oldJsonObj.vector = [] //this creates a new element into the object foreach(dataTable.getNumberOfRows()){ var x = {} x.id = XXX; ...
Read more >
Dynamic JSON - Retool Docs
Here are some examples of expressions that work (click on the JSON tab to see the evaluation): Normal ES5 ... Dynamically adding properties...
Read more >
Fetch data dynamically - Dart programming language
JSON is text based and human readable. The dart:convert library provides support for JSON. Use HttpRequest to dynamically load data. Web apps often...
Read more >
Append Json Files from a list of dynamic URLS
Append Json Files from a list of dynamic URLS. ‎12-02-2020 03:51 AM. Is this possible? The structure of the Json files are consistent....
Read more >
Creating and Modifying Dynamic Entities | Using JSON
The %FromJSON() method converts a JSON string to a dynamic entity. The following example constructs a dynamic array and serializes it to string...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found