0

選択入力フィールドを初期化する init 関数を持つオブジェクトがあります。

SelectBuilder.init();

初期化メソッドは次のようになります。

init: () => {
    const select = new MDCSelect(document.querySelector('.mdc-select'));
    select.listen('MDCSelect:change', async () => {
        if (select.value === state.data.selectedLocation) {
            return;
        }
        state.setState({ selectedLocation: select.value });
        let regions = geodata;
        let center = MAP_CENTER;
        let zoomLevel = window.screen.width < 600 ? 6 : 7;

        googleMap.panTo(center);
        map.setZoom(zoomLevel);
        drawRegions(googleMap, regions);
    });

    window.onload = async function () {
        console.log('loaded');
        const locations = await import('./assets/data/locations.json');
        const sortedLocations = locations.sort((a, b) => a.Name.localeCompare(b.Name));
        // init select items
        sortedLocations.forEach(location => {
            const list = document.querySelector('.mdc-list');
            const newListItem = document.createElement('li');
            newListItem.setAttribute('class', 'mdc-list-item');
            newListItem.setAttribute('data-value', location.ID);

            const listeItemIcon = document.createElement('i');
            listeItemIcon.setAttribute('class', 'fas fa-map-marker-alt');
            const listeItemText = document.createElement('span');
            listeItemText.setAttribute('class', 'mdc-list-item__text');

            listeItemText.appendChild(document.createTextNode(location.Name));
            newListItem.appendChild(listeItemIcon);
            newListItem.appendChild(listeItemText);
            list.appendChild(newListItem);
        });
    };
}

ページが完全に読み込まれるまで選択メニュー項目の構築を延期したいので、それをウィンドウの onload イベントでラップしました。しかし、コンソールでイベントが発生しないことがわかります。ここで何が間違っていますか?

4

0 に答える 0