🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: pl.comp.lang.javascript
Article #51997

JA a tworzenie elementów DOM

#51997
From: Roman Tyczka
Date: Tue, 29 Jan 2019 13:53
45 lines
2355 bytes
Wyczytałem, że tworzenie struktury dokumentu przez takie "html stringi"
jest złe:

data.forEach(function(entry) {
	$('#filelist').append(
        `<li class="list-group-item">
           <strong class="bundle-info">plik: 
            <a href="${entry.URL}">${entry.OriginalName}</a>
           </strong> - <strong class="bundle-info">
           ${entry.Desc}</strong>
         </li>`
	}

I że powinno się obiektowo i w ogóle, zrobiłem tak:

data.forEach(function(entry) {
	let li = document.createElement('li');
	li.className = 'list-group-item';
	let strong = document.createElement('strong');
	strong.className = 'bundle-info';
	strong.appendChild(document.createTextNode('plik: '));
	let a = document.createElement('a');
	a.href = `${entry.URL}`;
	a.appendChild(document.createTextNode(`${entry.OriginalName}`));
	strong.appendChild(a);
	li.appendChild(strong);
	li.appendChild(document.createTextNode(' - '));
	strong = document.createElement('strong');
	strong.className = 'bundle-info';
	strong.appendChild(document.createTextNode(`${entry.Desc}`));
	li.appendChild(strong);
	frag.appendChild(li);
});

I teraz się zastanawiam... po pierwsze zajęło mi to w pip więcej czasu, po
drugie jest to mniej jednak czytelne, bo za cholerę z tego kodu nie widzę
struktury, po trzecie jakiekolwiek zmiany będą wymagały rozkminy co w czym
i pod czym siedzi... czy naprawdę tak się robi czy coś źle mi się
zrozumiało? A jeśli tak się robi to może jest jakaś zgrabniejsza forma
zapisu niż tona zmiennych?

-- 
pozdrawiam
Roman Tyczka
Technical Details
Message-ID: <11pc55hmknr5j.dlg@tyczka.com>
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!.POSTED!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.icm.edu.pl!newsfeed2.atman.pl!newsfeed.atman.pl!.POSTED!not-for-mail
From: Roman Tyczka <noemail@because.no>
Newsgroups: pl.comp.lang.javascript
Subject: JA a tworzenie elementów DOM
Date: Tue, 29 Jan 2019 14:53:56 +0100
Organization: ATMAN - ATM S.A.
Lines: 45
Sender: romek@tyczka.no.found
Message-ID: <11pc55hmknr5j.dlg@tyczka.com>
NNTP-Posting-Host: ip-94-75-90-137.wave.com.pl
Mime-Version: 1.0
X-Trace: node2.news.atman.pl 1548770036 22080 94.75.90.137 (29 Jan 2019 13:53:56 GMT)
X-Complaints-To: usenet@atman.pl
NNTP-Posting-Date: Tue, 29 Jan 2019 13:53:56 +0000 (UTC)
User-Agent: 40tude_Dialog/2.0.15.84
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit