Javascript Templating


Published on

This was a presentation I wrote for a previous employer, aimed at Java developers. The goal was to help them understand some of the new UI conventions I was putting into their application, so that they would be able to develop "smarter" JSP code.

  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Javascript Templating

  1. 1. Javascript & Templates Ideas to Inspire Better Coding
  2. 2. Overview <ul><ul><li>Javascript Objects </li></ul></ul><ul><ul><li>Prototype statement </li></ul></ul><ul><ul><li>Event delegation </li></ul></ul><ul><ul><li>Examples </li></ul></ul><ul><ul><li>JSON </li></ul></ul><ul><ul><li>Javascript templates </li></ul></ul><ul><ul><li>Putting it all together </li></ul></ul><ul><ul><li>Questions </li></ul></ul><ul><ul><li>Suggestions </li></ul></ul>
  3. 3. First, a good JavaScript site <ul><ul><li>Want to become a better JavaScript developer? Here is a good link, filled with all sorts of good understanding of how to write better code. </li></ul></ul>
  4. 4. Javascript Objects <ul><ul><li>Object types: </li></ul></ul><ul><ul><ul><li>Object </li></ul></ul></ul><ul><ul><ul><li>Date </li></ul></ul></ul><ul><ul><ul><li>Array </li></ul></ul></ul><ul><ul><ul><li>Function </li></ul></ul></ul><ul><ul><ul><li>Event </li></ul></ul></ul><ul><ul><li>New instances of Object types created using: </li></ul></ul><ul><li>var w = new Object(); //can contain anything </li></ul><ul><li>var x = new Date(); //value = today date AND time </li></ul><ul><li>var y = new Array(…); //index values can be specified </li></ul><ul><li>var z = new Function(…) {…} //objects can be passed in, function accessed like a variable (ie, no “()” after ‘y’ </li></ul>
  5. 5. Primitive Types <ul><ul><li>Primitive Types: </li></ul></ul><ul><ul><ul><li>undefined </li></ul></ul></ul><ul><ul><ul><li>number </li></ul></ul></ul><ul><ul><ul><li>string </li></ul></ul></ul><ul><ul><li>New instances of Primitive types created using: </li></ul></ul><ul><li>var w = 5; //number </li></ul><ul><li>var x; //undefined </li></ul><ul><li>var y = “3”; //string </li></ul><ul><li>var z = 4; //number </li></ul><ul><ul><li>Primitive types allow you to do silly things: </li></ul></ul><ul><li>w+z+y = “93”; </li></ul><ul><li>w+y+z = “534”; </li></ul><ul><li>x+w = NaN; //Not a Number (isNan() can test these) </li></ul><ul><li>isNan(y) = false; //string looks like number (quirk) </li></ul><ul><li>isNan(x) = true; </li></ul>
  6. 6. Determining Object Type <ul><ul><li>typeof is a statement you can use to determine which type of Object a variable is. </li></ul></ul><ul><li>var x = new Date(); </li></ul><ul><li>alert(typeof(x)); //displays “Date” </li></ul><ul><ul><li>This method is useful to determine if functions exist, variables are defined, etc. </li></ul></ul><ul><li>if(“function” == typeof(doSomething)) { </li></ul><ul><li>     doSomething(); </li></ul><ul><li>}else if(“undefined” = typeof(doSomething)) { </li></ul><ul><li>     //alert user, insert js library, etc </li></ul><ul><li>} </li></ul>
  7. 7. Associative Arrays <ul><ul><li>Associative Arrays are like HashMaps in Java. </li></ul></ul><ul><ul><li>They are typeof Object , not Array </li></ul></ul><ul><ul><li>Useful for keying data by Objects, rather than index. </li></ul></ul><ul><li>var mailArchive = {“msgID_43&quot;: &quot;Dear nephew, ...&quot;, </li></ul><ul><li>&quot;the second e-mail&quot;: “Blah...“, </li></ul><ul><li>/* and so on ... */}; </li></ul>
  8. 8. Associative Arrays (cont’d) <ul><li>var mailbox = {0: &quot; mail number 1)&quot;, </li></ul><ul><li>1: &quot;(mail number 2)&quot;, </li></ul><ul><li>2: &quot;(mail number 3)&quot;}; </li></ul><ul><ul><li>Note that Associative arrays are objects, so the values you put in them need to be name:value and followed by a comma (except for last one) </li></ul></ul><ul><ul><li>To iterate: </li></ul></ul><ul><li>for (ind in mailbox) </li></ul><ul><li>alert(&quot;Processing&quot; + ind + &quot;: &quot; + mailbox[ind]); </li></ul><ul><ul><li>This does not work: </li></ul></ul><ul><li>for (var ind = 0; ind <= mailbox.length; ind++) </li></ul><ul><li>//. length attribute is not defined for Object </li></ul>
  9. 9. Object Definition var mailbox = {0: &quot; mail number 1)&quot;, 1: &quot;(mail number 2)&quot;, 2: &quot;(mail number 3)&quot;}; OR, create your own object type : function FormObject(id) {      var obj = oForm[id];      obj.originalValue = obj.value;      obj.dirty = false;      obj.onchange = doSomething(); };
  10. 10. prototype statement <ul><ul><li>The prototype statement allows you to extend the functionality of an object. </li></ul></ul><ul><ul><ul><li>Custom variables </li></ul></ul></ul><ul><ul><ul><li>Custom methods </li></ul></ul></ul><ul><ul><ul><li>Custom events </li></ul></ul></ul><ul><ul><li>Think of a prototyped object like any Java Object that extends another Object. </li></ul></ul><ul><ul><li>Do not add prototypes to the global scope for native Javascript objects. Create your own object type! </li></ul></ul>
  11. 11. Extending Objects Date.prototype.format = function(vFormat) {     //'this' is just like in Java      alert(&quot;The date to format:&quot;+this);     var x = (// date formatting w/ ‘this’ & ‘vFormat’);     return x; }
  12. 12. Practical use of prototype <ul><ul><li>Overwriting browser specific methods to make the cross-browser </li></ul></ul><ul><ul><ul><li>window.showModalDialog (IE only) </li></ul></ul></ul><ul><ul><ul><li>window.prototype.showModalDialog can be coded to do a browser test and use IE implementation, and use custom code in Firefox. </li></ul></ul></ul><ul><ul><li>Adding useful methods to objects </li></ul></ul><ul><ul><li>Abstraction of functionality </li></ul></ul><ul><ul><li>Limiting scope of variables not used by other code </li></ul></ul><ul><ul><li>Building powerful custom object types </li></ul></ul><ul><ul><li>Anything that JavaScript can do, can be written into an object’s abilities (AJAX, DOM Manipulation, event delegation, etc) </li></ul></ul>
  13. 13. Questions?
  14. 14. Event Delegation <ul><ul><li>Events are fired when certain events occur in the browser. They can be captured with “Event Handlers”: </li></ul></ul><ul><ul><ul><li>onload : when the HTML code has been parsed and displayed by browser </li></ul></ul></ul><ul><ul><ul><li>onchange : when the value of a form object changes </li></ul></ul></ul><ul><ul><ul><li>onsubmit : when the form is submitted. (return false, if you want to stop the form submit) </li></ul></ul></ul><ul><ul><ul><li>onclick : when an page element is clicked on. </li></ul></ul></ul><ul><ul><ul><li>More… </li></ul></ul></ul><ul><ul><li>Event Handlers call JavaScript code, not necessarily functions. </li></ul></ul><ul><li><img src=“…” onclick=“doSomething(…)”/> </li></ul>
  15. 15. <ul><ul><li>Handler functions can be passed the event object </li></ul></ul><ul><li><img src=“…” onclick=“doSomething( event )”/> </li></ul><ul><ul><li>The event object is most useful for finding: </li></ul></ul><ul><ul><ul><li>The DOM object that triggered the event </li></ul></ul></ul><ul><ul><ul><li>The type of event that occurred </li></ul></ul></ul><ul><ul><li>getTargetElement finds the object the event was triggered by (i.e., an input that changed, button clicked, etc.). Once we have the element, we can traverse the DOM. </li></ul></ul><ul><li>function getTargetElement(evt) { </li></ul><ul><li>    evt = (evt)? evt: ((window.event) ? window.event : &quot;&quot;); </li></ul><ul><li>     var elem; </li></ul><ul><li>    if ( { </li></ul><ul><li>         elem = ( == 3) ? :; </li></ul><ul><li>     } else { </li></ul><ul><li>     elem = evt.srcElement; </li></ul><ul><li>    } </li></ul><ul><li>    return elem; </li></ul><ul><li>} </li></ul><ul><li>function doSomething(evt) { </li></ul><ul><li>    var oElem = getTargetElement(evt); </li></ul><ul><li>     //oElem.parentNode, oElem.innerHTML, oElem.getAttribute(…), etc </li></ul><ul><li>} </li></ul>
  16. 16. DOM (Document Object Model) <ul><ul><li>The DOM is the structure of the HTML document (or XML document). </li></ul></ul><ul><ul><li>It follows a data type definition (DTD) of strict, loose, transitional, defined by a <meta> tag. </li></ul></ul><ul><ul><li>You can adhere to the DOM DTD, but because it’s structure is understood, you can start adding attributes to elements. </li></ul></ul><ul><li><table id=“resources” onclick=“editTechnician(event)”> </li></ul><ul><li>  <tr PK=“335abc”><td>Bob Smith</td></tr> </li></ul><ul><li>  <tr PK=“3fe2452”><td>Sally Jones</td></tr> </li></ul><ul><li>  <tr PK=“abe325”><td>Marky Mark</td></tr> </li></ul><ul><li></table> </li></ul><ul><li><input name=“blah” isDirty=“false” origValue=“${…}” value=“${…}”/> </li></ul>
  17. 17. Events and DOM <ul><ul><li>Using the event capturing method, we can use the structured data that the DOM presents us to: </li></ul></ul><ul><ul><ul><li>Reduce the need to ‘re-request’ data </li></ul></ul></ul><ul><ul><ul><li>Remove JavaScript objects containing info already in the DOM (another reason why custom attributes are encouraged, more data available). </li></ul></ul></ul><ul><ul><ul><li>Reduce JavaScript code & function calls </li></ul></ul></ul><ul><li>function editTechnician(evt) { //user clicked “Bob Smith” </li></ul><ul><li>  var oElem = getTargetElement(evt); //TD or TR </li></ul><ul><li>  var oTR = getRow(oElem); //ensures we have the TR elem </li></ul><ul><li>  var userId = oTR.getAttribute(“PK”); //get PK attribute </li></ul><ul><li>  //do something with &quot;userID&quot; </li></ul><ul><li>} </li></ul>
  18. 18. Custom Objects, Events and prototype function FormObject(id) { //constructor pattern   //set properties to the form element's DOM object dynamically   var obj = oForm[id];     obj.originalValue = obj.value; //original value preserved   obj.dirty = false; //has object value changed?     //register an onchange to each input needing validation   obj.onchange = this.validate; } //assign validation function to FormObject's FormObject.prototype.validate = function(e) { //by doing it this way, you don't have to change any occurrence of //&quot;onChange&quot; attributes calling an updated function name var evt = e ? e : window.event; var oElem = oForm[]; if(oElem && oElem.getAttribute(&quot;validate&quot;)) { //test for null   var value = oElem.value;   //automatically update the dirty flag   oElem.dirty = (value != oElem.originalValue);   if(&quot;phone&quot; == oElem.getAttribute(&quot;validate&quot;))     validatePhone(value);   else if(&quot;email&quot; == oElem.getAttribute(&quot;validate&quot;))     validateEmail(value);   } }
  19. 19. Sample Code #1 <ul><ul><li>Example 1: </li></ul></ul><ul><ul><ul><li>validations.js </li></ul></ul></ul><ul><ul><ul><li>validate_old.html </li></ul></ul></ul><ul><ul><li>View in Browser </li></ul></ul><ul><ul><li>NOTE: If the event Handler changes it’s name, it is going to be tedious to find and change all references in the application. </li></ul></ul>
  20. 20. Sample Code #2 & 3 <ul><ul><li>Example 2: </li></ul></ul><ul><ul><ul><li>validations.js </li></ul></ul></ul><ul><ul><ul><li>FormObject.js (NOTE: object js files are Capitalized) </li></ul></ul></ul><ul><ul><ul><li>validate.html </li></ul></ul></ul><ul><ul><li>Example 3: </li></ul></ul><ul><ul><ul><li>validations.js </li></ul></ul></ul><ul><ul><ul><li>FormObject.js </li></ul></ul></ul><ul><ul><ul><li>validate_best.html </li></ul></ul></ul><ul><ul><li>View in Browser </li></ul></ul><ul><ul><ul><li>(Demonstrate how easy it is to change validate in source) </li></ul></ul></ul><ul><ul><ul><li>NOTE: Auto-Validation occurs as a behavior of FormObject objects </li></ul></ul></ul>
  21. 21. Questions?
  22. 22. JSON (pronounced Jason) <ul><ul><li>JSON === JavaScript Object Notation </li></ul></ul><ul><ul><li>A JSON object is just a Javascript Object that follows a very specific and SIMPLE format. </li></ul></ul><ul><ul><li>There is an RFC that exists for it. </li></ul></ul><ul><ul><ul><li>An “Request For Comments” (RFC) is a document that is working notes of the Internet Research and Development Community. </li></ul></ul></ul><ul><ul><ul><li>An RFC document may be on essentially any topic related to computer communication, and may be anything from a meeting report to the specification of a protocol standard. </li></ul></ul></ul><ul><ul><li>JSON documented on the net: </li></ul></ul><ul><ul><ul><li> </li></ul></ul></ul>
  23. 23. JSON Objects <ul><ul><li>JSON objects can be assembled by ANY language, but only useful when passed to Javascript at some point. </li></ul></ul><ul><ul><li>Some servers and servlets are already allowing the Java code the option to return data in JSON format. </li></ul></ul><ul><ul><ul><li>Struts 2 can do this </li></ul></ul></ul><ul><ul><ul><li>DWR returns a JavaScript object that can be easily transformed into a JSON object. </li></ul></ul></ul><ul><ul><ul><li>There is a JSON tag-library that I’m poking around with to see if it’s worth using in house. </li></ul></ul></ul>
  24. 24. JSON Format <ul><li>&quot;Image&quot;: { &quot;Width&quot;: 800, </li></ul><ul><li>     &quot;Height&quot;: 600, </li></ul><ul><li>     &quot;Title&quot;: &quot;View from 15th Floor&quot;, </li></ul><ul><li>     &quot;Thumbnail&quot;: { </li></ul><ul><li>         &quot;Url&quot;: &quot;/image/481989943&quot;, </li></ul><ul><li>           &quot;Height&quot;: 125, </li></ul><ul><li>                 &quot;Width&quot;: &quot;100“ </li></ul><ul><li>     }, </li></ul><ul><li>     &quot;IDs&quot;: [116, 943, 234, 38793] </li></ul><ul><li>} </li></ul><ul><ul><li>Note that JSON objects have a simple and rich structure. It is possible to convert structured objects (Java, XML, other) into JSON objects with the appropriate compiler or tools. </li></ul></ul><ul><ul><ul><li>JSON taglib </li></ul></ul></ul><ul><ul><ul><li>JSON for Java </li></ul></ul></ul>
  25. 25. <ul><ul><li>This is a JSON array containing two objects: </li></ul></ul><ul><li>[ </li></ul><ul><li>  { &quot;precision&quot;: &quot;zip&quot;, </li></ul><ul><li>     &quot;Latitude&quot;: 37.7668, </li></ul><ul><li>     &quot;Longitude&quot;: -122.3959, </li></ul><ul><li>     &quot;Address&quot;: &quot;&quot;, </li></ul><ul><li>     &quot;City&quot;: &quot;SAN FRANCISCO&quot;, </li></ul><ul><li>     &quot;State&quot;: &quot;CA&quot;, </li></ul><ul><li>     &quot;Zip&quot;: &quot;94107&quot;, </li></ul><ul><li>     &quot;Country&quot;: &quot;US&quot;}, //Note: comma delimited objects </li></ul><ul><li>  { &quot;precision&quot;: &quot;zip&quot;, </li></ul><ul><li>     &quot;Latitude&quot;: 37.371991, </li></ul><ul><li>     &quot;Longitude&quot;: -122.026020, </li></ul><ul><li>     &quot;Address&quot;: &quot;&quot;, </li></ul><ul><li>     &quot;City&quot;: &quot;SUNNYVALE&quot;, </li></ul><ul><li>     &quot;State&quot;: &quot;CA&quot;, </li></ul><ul><li>     &quot;Zip&quot;: &quot;94085&quot;, </li></ul><ul><li>     &quot;Country&quot;: &quot;US&quot; } </li></ul><ul><li>] </li></ul>
  26. 26. <ul><ul><li>Note that JSON format doesn’t natively follow a definition of it’s structure </li></ul></ul><ul><li>var tech = { </li></ul><ul><li>    “ name” : “Brian Ruhl”, </li></ul><ul><li>    “ branchId” : “abc_pest_4322”, </li></ul><ul><li>    “ jobs” : [ </li></ul><ul><li>         { “address” : “...”, </li></ul><ul><li>          “ time” : “15:30”, </li></ul><ul><li>          “ num_of_services” : 32 </li></ul><ul><li>         }, </li></ul><ul><li>         { “address” : “...”, </li></ul><ul><li>          “ time” : “17:30”, </li></ul><ul><li>          “ num_of_services” : 3, </li></ul><ul><li>           “ required” : true //Allowed, but sloppy </li></ul><ul><li>         } </li></ul><ul><li>     ], </li></ul><ul><li>    “ techId” : “352a3bd” </li></ul><ul><li>} </li></ul>
  27. 27. JSON in JSP <%@ taglib uri=&quot;/WEB-INF/c.tld&quot; prefix=&quot;c&quot;%> {       &quot;name&quot; : &quot;${}&quot;,      &quot;address&quot; : &quot;${initData.address}&quot;,      &quot;phone&quot; : &quot;${}&quot;,      &quot;serviceType&quot; : &quot;${initData.serviceType}&quot;,      &quot;serviceLength&quot; : ${initData.serviceLength},      &quot;prefTechs&quot; : [      <c:if test=&quot;${not empty initData.prefTechs}&quot;>          <c:forEach var=&quot;tech&quot; items=&quot;${initData.prefTechs}&quot;                      varStatus=&quot;status&quot;>        {          &quot;name&quot; : &quot;${tech.first}&quot;,          &quot;techId&quot; : &quot;${tech.second}&quot;        }<c:if test=&quot;${!status.last}&quot;>,</c:if>          </c:forEach>      </c:if>      ] }
  28. 28. AJAX & JSON <ul><ul><li>JSON can be generated by Java, JSP, taglibrary. </li></ul></ul><ul><ul><li>Responding to AJAX requests with JSON offers structured data that Javascript natively understands. </li></ul></ul><ul><ul><li>Currently, browsers don’t accept the “application/json” MIME type, but it’s ok to return it as “text/plain”. </li></ul></ul><ul><ul><li>When requesting JSON data via AJAX, the responseText must be evaluated: </li></ul></ul><ul><li>var j = eval('(' + XHRObj.responseText + ')'); </li></ul><ul><ul><li>The '{' operator is subject to a syntactic ambiguity in JavaScript: it can begin a block or an object literal. We wrap the text in parenthesis to eliminate the ambiguity. </li></ul></ul>
  29. 29. DWR and JSON <ul><ul><li>DWR defines how to convert Java objects into Javascript objects. These Javascript objects are in JSON format. </li></ul></ul><ul><ul><li>DWR “function call” makes AJAX request and response is returned as JSON object. </li></ul></ul><ul><ul><li>Data can be displayed two ways: </li></ul></ul><ul><ul><ul><li>oElem.innerHTML += “…”; </li></ul></ul></ul><ul><ul><ul><li>Templates </li></ul></ul></ul>
  30. 30. Questions?
  31. 31. Templates <ul><ul><li>Templates make code: </li></ul></ul><ul><ul><ul><li>Reusable </li></ul></ul></ul><ul><ul><ul><li>Maintainable </li></ul></ul></ul><ul><ul><ul><li>Easier to read and test </li></ul></ul></ul><ul><ul><ul><ul><li>Test scripts testing code in a fragment can be reused by other scripts </li></ul></ul></ul></ul><ul><ul><li>Tiles, <jsp:include>, <@page file=“…”> are some examples in JSP </li></ul></ul><ul><ul><li>Template support can be written in Javascript, and it already has… </li></ul></ul>
  32. 32. Why use Javascript templates? <ul><ul><li>Cacheable! </li></ul></ul><ul><ul><li>If you need JavaScript code to generate a lot of HTML (or even new JavaScript), you’re probably familiar with seeing more complex variations of escaped code, than my example below: </li></ul></ul><ul><li>'<a onclick=&quot;skillDeleteRows('+array[0]+');&quot; style=&quot;cursor: pointer&quot;><img src=&quot;/images/button_trash.gif“ alt=&quot;Remove&quot;/></a>'+'<input type=&quot;hidden&quot; name=&quot;skillIds&quot; value=&quot;'+array[0]+'&quot;/>'; </li></ul><ul><ul><li>By using a template, you can make this code look more like code (no escaping, no “+” stuff, etc): </li></ul></ul><ul><li><a onclick=&quot;skillDeleteRows(${});&quot; style=&quot;cursor:pointer;&quot;> </li></ul><ul><li>  <img src=&quot;/images/button_trash.gif&quot; alt=&quot;Remove&quot;/> </li></ul><ul><li></a> </li></ul><ul><li><input type=&quot;hidden&quot; name=&quot;skillIds&quot; value=&quot;${}&quot;/> </li></ul>
  33. 33. template.js library <ul><ul><li>A library that allows you to format HTML code into a syntax VERY close to JSTL </li></ul></ul><ul><ul><li>Documentation </li></ul></ul><ul><ul><li>Code can be reusable, but implementation requires you to put template fragment inside of a <textarea> ( dumb ) </li></ul></ul><ul><ul><ul><li>Dumb : We now have these <textarea>s that are submitted by the form that shouldn’t be </li></ul></ul></ul><ul><ul><ul><li>The reason for putting this in a <textarea> was so that the template could be extracted without risking modification by parsing (ie, Javascript) </li></ul></ul></ul><ul><ul><ul><li>Dumb : Once the pattern is parsed and the data object is compiled through it, we get a HTML string, which 99% of the time we want to write to an element on the page. However, to do the ‘write’ to the page, we need to write another line to do the innerHTML write. </li></ul></ul></ul><ul><ul><ul><li>Able to access other Javascript objects (functions, variables, etc) </li></ul></ul></ul>
  34. 34. Using template.js <ul><ul><li>Don’t, unless you need to parse the generated HTML string one more time...for some reason. </li></ul></ul><ul><ul><li>Requires: </li></ul></ul><ul><ul><ul><li>JSON data object </li></ul></ul></ul><ul><ul><ul><li>template.js included on page </li></ul></ul></ul><ul><ul><ul><li><textarea id=“…” style=“display:none”> containing the pattern </li></ul></ul></ul><ul><li>var s = TrimPath.processDOMTemplate(templateId, jsonDataObj); </li></ul><ul><li>var l = document.getElementById(IdOfWriteLocation); </li></ul><ul><li>if(l){ </li></ul><ul><li>   l.innerHTML = s; </li></ul><ul><li>} </li></ul>
  35. 35. exttemplate.js <ul><ul><li>Extends template.js parser objects </li></ul></ul><ul><ul><li>Retrieves template pattern file via AJAX </li></ul></ul><ul><ul><li>Able to access other Javascript objects (functions, variables, etc) </li></ul></ul><ul><ul><li>Resources required </li></ul></ul><ul><ul><ul><li>template.js </li></ul></ul></ul><ul><ul><ul><li>exttemplate.js </li></ul></ul></ul><ul><ul><ul><li>prototype.js (NOTE: This should not be confused with the prototype statement, it’s a powerful JS library with an unfortunately confusing name) </li></ul></ul></ul>
  36. 36. Using exttemplate.js <ul><ul><li>Template data patterns follow those of template.js </li></ul></ul><ul><ul><li>Method call requires: </li></ul></ul><ul><ul><ul><li>JSON data object </li></ul></ul></ul><ul><ul><ul><li>JST template file location </li></ul></ul></ul><ul><ul><ul><li>ID of HTML element to write compiled template source into </li></ul></ul></ul><ul><ul><li>We name the template files with an extension of .jst (JavaScript Template), to distinguish them from JSPs and other fragments. </li></ul></ul><ul><li>TrimPath.processTemplateFile('/<filepath>.jst', jsonObj,             document.getElementById(idOfWriteLocation)); </li></ul>
  37. 37. Example: Javascript template <ul><ul><li>Javascript template example: customerPrefDetails.jst </li></ul></ul><ul><ul><li>Documentation for template.js </li></ul></ul>
  38. 38. Putting it all Together <ul><ul><li>Currently, the “Preferred Technician” Map is using DWR, JSON, and Javascript Templates . </li></ul></ul><ul><li>/** </li></ul><ul><li>* onClick event for a dot, gets the customer preference </li></ul><ul><li>* data from DWR </li></ul><ul><li>*/ </li></ul><ul><li>function getCustomerDetails(customerId) { </li></ul><ul><li>     $('customerDetails').update(); </li></ul><ul><li>     PrefTechDWR.getCustomerDetails(customerId, </li></ul><ul><li>     {callback:function(xferObj) { </li></ul><ul><li>         displayCustomerData(xferObj)}, </li></ul><ul><li>         errorHandler:function(str){ </li></ul><ul><li>             alert(&quot;there was an issue with the server:&quot;+str)}, </li></ul><ul><li>         asynch:false </li></ul><ul><li>     }); </li></ul><ul><li>} </li></ul><ul><li>/** </li></ul><ul><li>* Given a customer object, draw the data using trimpath </li></ul><ul><li>* into the page. </li></ul><ul><li>*/ </li></ul><ul><li>function displayCustomerData(customer) { </li></ul><ul><li>     var oObj = {customer:customer}; </li></ul><ul><li>     TrimPath.processTemplateFile('/configuration/customerPrefDetails.jst', </li></ul><ul><li>         oObj, $('customerDetails')); </li></ul><ul><li>} </li></ul>
  39. 39. Questions?
  40. 40. Suggestions <ul><ul><li>How could I have made this better? </li></ul></ul><ul><ul><li>Was it easy to understand? </li></ul></ul><ul><ul><li>What would you like to learn more about? </li></ul></ul><ul><ul><li>… less about? </li></ul></ul>