Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // =========================
- // Utilities
- // =========================
- var utils = utils || {};
- // ========================================
- // Load template file into the DOM webpage
- // Params:
- // url - url to the template file (starting from the index.html directory)
- // args - parameters to be put into the template, on the template mark
- // them with double curly braces like: {{argumentName}}
- // override - OPTIONAL override the previous template ( default: false )
- // callback - OPTIONAL callback function run after template is loaded
- //
- // ========================================
- utils.loadTemplate = (function(f,u,n,c){
- var $templateDom = $('#dynamic-template'),
- $ajaxCall;
- if( typeof n === 'function' && typeof c === 'undefined' ) c = n;
- $ajaxCall = $.get(f).success(function(response){
- var mustachedTemplate = Mustache.render(response,u);
- if( n === true ){
- $templateDom.html(mustachedTemplate);
- } else {
- $templateDom.append(mustachedTemplate);
- }
- if( typeof c === 'function' ){
- c(mustachedTemplate);
- }
- }).fail(function(response){
- console.warn('Template error',response);
- });
- return true;
- });
- // ========================================
- // Load a json file from the back-end ( via GET )
- // Params:
- // url - url/path to the external, back-end server
- // callback - OPTIONAL callback function for the request
- //
- // ========================================
- utils.getData = utils.loadJSON = (function(f,u){
- var $ajaxCall,
- json;
- $ajaxCall = $.get(f).success(function(response){
- if( typeof response === 'String'){
- try {
- json = JSON.parse(response);
- } catch(exception){
- console.warn('JSON error',exception);
- }
- } else {
- json = response;
- }
- if( typeof u === 'function' ){
- u( json || null );
- }
- }).fail(function(response){
- console.warn('JSON error',response);
- });
- });
- // ========================================
- // Send data to the back-end ( via POST )
- // Params:
- // url - url/path to the external, back-end server
- // data - parameters sent to the back-end
- // callback - OPTIONAL function called after the back-end responds
- //
- // ========================================
- utils.sendData = utils.sendJSON = (function(f,u,n){
- var $ajaxCall;
- $ajaxCall = $.post(f,u).success(function(response){
- if( typeof response === 'String'){
- try {
- json = JSON.parse(response);
- } catch(exception){
- console.warn('JSON error',exception);
- }
- } else {
- json = response;
- }
- if( typeof u === 'function' ){
- u( json || null );
- }
- }).fail(function(response){
- console.warn('POST error',response);
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement