json对象中的把手?

时间:2017-12-06 01:37:56

标签: jquery json handlebars.js

我正在尝试将把手模板存储在json对象中。我想在另一个函数中编译它,但第一个函数的输出给我[object Object]。我无法将其转换回车把模板进行编译。当我为响应执行console.log()时,它将作为hansdlebars模板返回。见下文:

//Object to hold compile all templates output
var tmplatesJSON = "";

 //This function compiles all the external templates in the filesArray, and adds them to the 'main' tag in the html.
(function( $ ){
   $.fn.compileAllTemplates = function(templateNames) {
        //loop through all templates in array
            //var tmplJSON = '[{"template": [';
            var tmplJSON = '[';
            for (var i=0; i<templateNames.length; i++) {
                //console.log(templateNames.length);
                //for every template load it
                var template = templateNames[i];
                var templ = $.get(template, function (response){

                    var templ = response;
                    return templ;
                });

                tmplJSON += '{"tmplID": "'+templateNames[i]+'",';
                tmplJSON += '"tmplHTML": "' + templ +'"}';    
                console.log(templ);    
                    //this makes sure there isn't a ',' on the last one.
                    var j = templateNames.length - 1;
                    if(j!=i){
                        tmplJSON += ',';
                    } else{
                        tmplJSON += '';

                    }     
            }
            tmplJSON += ']';

            templatesJSON = tmplJSON;
            templatesJSON = JSON.parse(templatesJSON); 
   }; 
})( jQuery ); 

$(document).compileAllTemplates(templatesArray);



//This function 
(function( $ ){
   $.fn.renderTemplates = function(templateName, dest) {
        //find the appropriate template id in the templatesJSON JSON object
        for (var i=0; i<templatesJSON.length; i++) {
            //console.log(templatesJSON[i].tmplID);
            var str = templatesJSON[i].tmplID.toString();
            if(str === templateName) {
                //console.log(templatesJSON[i].tmplHTML);



                /***********************
                TODO: Need to convert the object sent in the json to something consumable to Handlebars
                ***********************/

                //Render the compiled template to the specified html element
                var destination = $(document).find(dest);
                $(destination).append(templatesJSON[i].tmplHTML); 
                return true;
            } 
        }


   }; 
})( jQuery );               
$(document).renderTemplates('templates/header.hbs', 'header');   
$(document).renderTemplates('templates/nav-main.hbs', 'nav');
$(document).renderTemplates('templates/page-title.hbs', '.page-title');
$(document).renderTemplates('templates/footer.hbs', 'footer');

jsonReturned = [{"tmplID": "templates/header.hbs","tmplHTML": "[object Object]"},{"tmplID": "templates/page-title.hbs","tmplHTML": "[object Object]"},{"tmplID": "templates/nav-main.hbs","tmplHTML": "[object Object]"},{"tmplID": "templates/footer.hbs","tmplHTML": "[object Object]"}]

0 个答案:

没有答案
相关问题