Mustache Helper functions

Recently, implemented the Mustache Template processing framework.  As i worked through this implementation, i found myself creating the same JavaScript call and decided to simplify the overall execution into a single function signature and just call it multiple times.

Root Mustache Call:

1:  /*********************************************************************  
2:     @file - string value, Should be the HTML Template file.  
3:     @filterid - string value, Should the ID value of the Script tag inside  
4:           @file to obtain the HTML content.  
5:     @api - string value, Should be the URL to the API request  
6:     @dest - string value, Should be the destination selector, ideally ID  
7:           value of the control to load the Mustache Template into.  
8:     @func - (Nullable) Function value, If defined this will be executed  
9:           after the loading of the data into the Template.  
10:  ----------------------------------------------------------------------  
11:     LoadTemplate('...', '#...', 'api/.../...', "#...", function(){...});  
12:  *********************************************************************/  
13:  function LoadTemplate(file, filterid, api, dest, func) {  
14:     GetJsonUTF8(api, function (resp) {  
15:        $.get(file, function (template) {  
16:           var tmplt = $(template).filter(filterid).html();  
17:           $(dest).html(Mustache.render(tmplt, JSON.parse(resp)));  
18:           //Execution post-load methodology  
19:           if (typeof func !== 'undefined') {  
20:              if (typeof func === 'function') {  
21:                 func();  
22:              }  
23:           }  
24:        });  
25:     });  
26:  }  

Calling Functions:

1:  /*********************************************************************  
2:     @api - string value, Should be the URL to the API request  
3:     @dest - string value, Should be the destination selector, ideally ID  
4:           value of the control to load the Mustache Template into.  
5:     @func - (Nullable) Function value, If defined this will be executed  
6:           after the loading of the data into the Template.  
7:  ----------------------------------------------------------------------  
8:     LoadDropDownList('api/.../...', "#...", function(){...});  
9:  *********************************************************************/  
10:  function LoadDropDownList(api, dest, func) {  
11:     LoadTemplate("Templates/DropDownList.html", "#ddlTempl", api, dest, func);  
12:  }  
13:  function LoadTeamDropDownList() {  
14:     LoadDropDownList('api/teams/get', "#teams");  
15:  }  
16:  function LoadMagnitudeDropDownList(txt) {  
17:     LoadDropDownList('api/magnitudes/get', "#taskdetailsmagnitudes", function () {  
18:        $("#taskdetailsmagnitudes select option").filter(function () {  
19:           return $(this).text() == txt;  
20:        }).prop('selected', true);  
21:     });  
22:  }  

Hopefully this will help you on implementations and help in stream-lining new Template implementation.

Comments

Popular posts from this blog

SysInternals - BgInfo for ALL Users

JSON/AJAX Helpers

Acquiring List of controls - Classic JS