Skip to content

Instantly share code, notes, and snippets.

@rajeshsegu
Last active December 3, 2020 05:42
Show Gist options
  • Select an option

  • Save rajeshsegu/4030445 to your computer and use it in GitHub Desktop.

Select an option

Save rajeshsegu/4030445 to your computer and use it in GitHub Desktop.

Revisions

  1. rajeshsegu revised this gist Feb 22, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion handlebars-helper.js
    Original file line number Diff line number Diff line change
    @@ -50,7 +50,7 @@

    //Handle loading multiple templates
    $('script[type="'+Handlebars.Templates.TYPE+'"]')
    .forEach(function(handlebar){
    .each(function(handlebar){

    Handlebars.Templates.processTemplate(handlebar);

  2. rajeshsegu revised this gist Nov 7, 2012. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions handlebars-helper.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@

    //Handlebars Template helper
    (function(Handlebars){

    @@ -86,7 +85,7 @@
    };
    })(Handlebars);

    //Handlebars Basic Loader
    //Basic Handlebars Loader
    (function(Handlebars, $){

    Handlebars.Loader = {
    @@ -111,6 +110,13 @@

    //Process Templates
    Handlebars.Templates.process();

    //Final callback
    if(args.callback){
    args.callback();
    }


    }
    })
    .error(function(response){
  3. rajeshsegu renamed this gist Nov 7, 2012. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion handlebar-utils.js → handlebars-helper.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@

    //Handlebars Template helper
    (function(Handlebars){

    Handlebars.Templates = {
    @@ -84,7 +86,7 @@
    };
    })(Handlebars);


    //Handlebars Basic Loader
    (function(Handlebars, $){

    Handlebars.Loader = {
  4. rajeshsegu revised this gist Nov 7, 2012. No changes.
  5. rajeshsegu renamed this gist Nov 7, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  6. rajeshsegu created this gist Nov 7, 2012.
    120 changes: 120 additions & 0 deletions handlebar-utils
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,120 @@
    (function(Handlebars){

    Handlebars.Templates = {

    //script type of handle bar template
    TYPE: "text/x-handlebars-template",

    //Handlebar identifier attribute
    NAME: "data-template-name",

    //Template Store.
    _store: {},


    //Get the template from template store
    get: function(templateName, context){

    var template = this._store[templateName];
    if(context){
    template = template(context);
    }
    return template;

    },

    //Add the template to the template store.
    add: function(templateName, templateContent){

    var template = Handlebars.compile(templateContent);

    this._store[templateName] = template;

    return template;

    },

    //Remote the template from the Templates store
    remove: function(templateName){

    this._store[templateName] = null;
    delete this._store[templateName];

    },

    //Process the entire DOM for handlebar templates
    process: function(el){

    var found = false;

    //Handle loading multiple templates
    $('script[type="'+Handlebars.Templates.TYPE+'"]')
    .forEach(function(handlebar){

    Handlebars.Templates.processTemplate(handlebar);

    found = true;

    }
    );

    return found;
    },

    //Process individual handlebar script
    processTemplate: function(el){

    //Access the handlebar template
    var handlebarTemplate = $(el),
    template;

    //Add template to templateStore
    template = Handlebars.Templates.add(
    handlebarTemplate.attr(Handlebars.Templates.NAME),
    handlebarTemplate.text()
    );

    //Cleanup DOM once processed
    handlebarTemplate.remove();

    return template;

    }

    };
    })(Handlebars);


    (function(Handlebars, $){

    Handlebars.Loader = {

    load: function(args){
    args = args || {};

    $.ajax({
    url: args.path,
    async: !args.sync,
    type: "GET",
    cache: true
    }, "html")
    .success(function(data, status ,response){

    if(status == "success"){

    data = response.responseText;

    //Append to make it part of DOM
    $('body').append(data);

    //Process Templates
    Handlebars.Templates.process();
    }
    })
    .error(function(response){
    alert("Failed to load templates @ " + args.path);
    });
    }
    }

    })(window.Handlebars, jQuery);