Solved

Using templates in javascript instead of server side

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

825 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