Solved

Using templates in javascript instead of server side

Posted on 2016-09-13
2
24 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
  • 2
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
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
Comment Utility
do you need more ? did I miss something ?
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now