Solved

Using templates in javascript instead of server side

Posted on 2016-09-13
2
51 Views
Last Modified: 2016-09-28
HI,
In my js code currently i have the following :
var htmlstring = $.parseHTML(htmlstring);
   $('body').append(htmlstring
This html string is created on server side from an html template... and looping over a list.

I wonder what are the performance issues this could cause if the html string i huge : $.parseHTML(htmlstring)
Other way i can think of is return an array of json objects.. And then create DOM on client side...
Dont know if creating a template on client side is possible ?
Also will that be easy for browser ?

pointifying some things :
1) There is an ajax call from client which returns an htmlString
This htmlString is created from a list and an html template
2) Performance isues with $.parseHTML(htmlstring)
3) How do i do the same thing by returning the json and creating html on client side.. some way so that browser does not probably have to create any DOM as template structure is already there.

Here is the template i am using on server side :
<div th:each="testList : ${testList}" class="test"
     th:attr="data-id=${testList.id},data-chat-id=${testList.conversationId},data-show-all=${showAll}">
  <div class="caption">
    <span th:if="${showAll} == true" th:text="'In: '+${testList.chatName}"/>
    <span th:if="${showAll} == false" th:text="${testList.userName}"/>
    <span th:text="${testList.date}"/>
  </div>
  <h3 th:text="${testList.title}"/>
  <div class="xyz"
       th:utext="${testList.preview}"/>
</div>

Open in new window


Thanks
0
Comment
Question by:Rohit Bajaj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 41797164
Check this page : https://docs.angularjs.org/tutorial/step_02
Welcome to angular.
In the example you can see we use this :
    <li ng-repeat="phone in phones">
      <span>{{phone.name}}</span>
      <p>{{phone.snippet}}</p>
    </li>

Open in new window

and this json :
  $scope.phones = [
    {
      name: 'Nexus S',
      snippet: 'Fast just got faster with Nexus S.'
    }, {
      name: 'Motorola XOOM™ with Wi-Fi',
      snippet: 'The Next, Next Generation tablet.'
    }, {
      name: 'MOTOROLA XOOM™',
      snippet: 'The Next, Next Generation tablet.'
    }
  ];

Open in new window

to create a nice list
0
 
LVL 82

Expert Comment

by:leakim971
ID: 41819746
do you need more ? did I miss something ?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question