2013-05-02 108 views
0

我想通过Ajax-请求将Handlebars模板按需交付到我的Ember.js应用程序。我能够编译它的服务器上,我也能为String提供类似以下的输出(function):通过JSON按需提供预编译的把手模板

Ember.TEMPLATES["authentication"] = Ember.Handlebars.template(function anonymous(Handlebars,depth0,helpers,partials,data) { 
this.compilerInfo = [2,'>= 1.0.0-rc.3']; 
helpers = helpers || Ember.Handlebars.helpers; data = data || {}; 
    var buffer = '', stack1, hashTypes, options, helperMissing=helpers.helperMissing, escapeExpression=this.escapeExpression; 


    data.buffer.push("<h1>Hooray! It works!</h1>\r\n"); 
    hashTypes = {}; 
    options = {hash:{},contexts:[depth0],types:["ID"],hashTypes:hashTypes,data:data}; 
    data.buffer.push(escapeExpression(((stack1 = helpers.outlet),stack1 ? stack1.call(depth0, "main", options) : helperMissing.call(depth0, "outlet", "main", options)))); 
    return buffer; 

}); 

这正是String我能够从得到收到JSON对象。现在,我想这个预编译模板添加到Ember.TEMPLATES对象是这样的:

if(typeof data.template === 'string' && data.template != '') { 
        var escapedTemplateString = 
         data.template.replace(/\\n/g, "\\n").replace(/\\r/g, "\\r").replace(/\\t/g, "\\t"); 

        Ember.TEMPLATES[templateName] = Ember.Handlebars.template(new Function(escapedTemplateString)); 
       } 

但这种包装整个“字符串化”功能到另一个anonymous function(){}和我没有得到任何模板。如果我用eval解压'字符串化'功能,那么模板是undefined ...

有没有人知道如何得到一个function没有任何从'字符串化'函数包装? 非常感谢您的时间提前;)

回答

0

没关系......我忘了声明我Ajax请求作为async: false,使渲染过程将等待要加载的模板。

也,我改变了函数调用从eval(escapedTemplateString)eval("(" + escapedTemplateString + ")")

- >一切的现在工作的伟大