简体   繁体   English

在Atom.io包中加载HTML文件

[英]Loading HTML file in Atom.io package

I am creating an Atom.io package, but I am having some trouble loading the HTML for my GUI. 我正在创建Atom.io程序包,但在为GUI加载HTML时遇到了一些麻烦。

What I did was to create a new HTML file to contain the basic GUI: 我所做的是创建一个包含基本GUI的新HTML文件:

<atom-panel class='modal'>
    <h2>Please select file.</h2>
    <div name='open-file-list' class='select-list'>
        <ol />
    </div>

    <br />
    <div class='block'>
        <button name='cancelButton' class='inline-block'>Cancel</button>
        <button name='okButton' class='inline-block'>Ok</button>
        <button name='openFileButton' class='inline-block'>File not listed</button>
    </div>

</atom-panel>

And I am trying to load this into my package-view.js, however all that I get is an empty file. 我试图将其加载到我的package-view.js中,但是我得到的只是一个空文件。 My code is the following: 我的代码如下:

    constructor(serializedState)
    {    
        // Create root element
        this.element = document.createElement('div');
        this.element.classList.add('srt-helper');

        let fileReader = new FileReader();
        fileReader.onload = function(e)
        {
            console.log('File loaded...')
            let result = fileReader.result;
            console.log(result);
        }
        let f = new File([''], '../views/file-select-panel.html');
        fileReader.readAsText(f);
    }

Please note that I am using javascript and not coffe script. 请注意,我使用的是JavaScript,而不是咖啡脚本。 Afterwards I am planning to create a new div object and add the contents of my file to it as an inner html. 之后,我计划创建一个新的div对象,并将文件内容作为内部html添加到该对象中。

Is this the best way to do it? 这是最好的方法吗? If so, what I am doing wrong? 如果是这样,我在做什么错?

Note: I created a folder for the html file called: views. 注意:我为html文件创建了一个文件夹:views。

I've solved the problem with the following code: 我用以下代码解决了这个问题:

/*
 * Load a file using XMLHttpRequest.
 * Returns a promise that can be used for the loading.
 */
getFileLoadPromise(fileUrl)
{
    let promise = new Promise((resolve, reject) =>
    {
        let request = new XMLHttpRequest();
        request.open('GET', fileUrl);

        request.onload = () =>
        {
            if (request.status === 200)
                resolve(request.responseText);

            else
                reject(Error(request.statusText));
        };

        request.onError = () =>
        {
            reject(Error('Network error.'))
        };

        request.send();
    });

    return promise;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM