E
E
Extramezz2015-10-29 16:42:18
JavaScript
Extramezz, 2015-10-29 16:42:18

What is the best way to create blocks from data?

There is this HTML:

<template id="ctmp">
  <div>
    .........Куча дочерних и вложенных в них элементов.......
  </div>
</template>
<div id="dump">
  <!--Блок для вывода-->
</div>


I receive JSON data - text, titles, links, in general, I process all this like this:
var cloneNode = $(ctmp.content.children[0]);
//Получил json
var newBox = document.createDocumentFragment();
json.forEach(function(article){
  var newArticle = cloneNode.clone();
  cloneNode.find(".contentBox").text(article.text);
  cloneNode.find(".contentTitle").text(article.name);
  //И примерно 10 подобных манипуляций
  newBox.appendChild(cloneNode);
}
dump.appendChild(newBox);


In general, it takes 500-700ms to enumerate and create 50 new blocks, I won’t say that this is a lot, I’m interested in something else - is my approach correct? Maybe you organize these things in a simpler way? For example, you work not with an element, but with HTML code and simply replace the necessary parts with replace?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Ineshin, 2015-10-29
@Extramezz

Use a templating engine, the best way to turn JSON data into HTML, as optimized as possible.
- Handlebars.js
- Dust.js

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question