Solved

Using templates in javascript instead of server side

Posted on 2016-09-13
2
54 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

726 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