2014-06-20 42 views
0

我需要在javascript中获得句柄模板。 所以我创建TPL文件夹模板文件和XML来自javascript的网络资源

<resource type="download" name="tpl/" location="/tpl"/> 

写了这样行,如果我把一些图像到这个文件夹,我可以从CSS

.css 
{ 
background: url(tpl/image.png); 
} 

得到它,如果我要得到这个图片来自js AJS.$("css").css("background", "url(tpl/image.png)")我有错误 - 文件未找到;

图像文件是例如。在真正的我需要模板

AJS.$.ajax({ 
        url: "tpl/backlog_coll.handlebars", 
        cache: true, 
        success: function(data) { 
         source = data; 
         template = Handlebars.compile(source); 
         $('#backlog_coll').html(template); 
        } 
       }); 

回答

0

这是我会怎么做,

我会写一个一般的js访问文件的功能(允许调用它如下图所示

现在Global.js)GLOBAL.JS

function fnGetTemplate(strName) { 
if (Handlebars.templates === undefined || Handlebars.templates[strName] === undefined) { 
$.ajax({ 
    url : "tpl" + strName + ".handlebars", 
    success : function(data) { 
     if (Handlebars.templates === undefined) { 
      Handlebars.templates = {}; 
     } 
     Handlebars.templates[strName] = Handlebars.compile(data); 
    }, 
    async : false 
}); 
} 
return Handlebars.templates[strName]; 
} 

这是assumin g我有handlebars js库(类似handlebars-v1.3.0.js)适当引用。

然后在视图,我需要的模板,以显示里面,我将宣布模板如下图所示

template: fnGetTemplate("backlog_coll"); 

里面我的观点的渲染功能,我会叫这个模板提供所需的数据显示下面

render: function() { 
    this.$el.html(this.template(data)); 
} 

希望这有助于

+0

这种方法不发现模板了。它应该像'/jira/s/ru_RU-g6ghuq/773/3/1.0-SNAPSHOT/_/download/resources/com.swan.agile.swan-agile:swan-agile-scrumboard/tpl/“+ strName + “.handlebars' – waldemar

+0

实际上,在我的例子中,为了便于解释,我将路径硬编码为”tpl“。实际上,您应该将其作为变量值取出,以便您可以动态地填充该模板的路径,如下所示: 函数fnGetTemplate(strPath,strName)if(Handlebars.templates === undefined || 模板:fnGetTemplate(strPath,strName);这个模板可以用来创建一个或多个模板。 – eshcol