【问题标题】:Web-resources from Javascript来自 Javascript 的网络资源
【发布时间】:2014-06-20 12:01:57
【问题描述】:

我需要在 Javascript 中获取车把模板。 所以我在 tpl 文件夹中创建了模板文件,并在 ML 中写了这样一行:

<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);
                    }
                });

【问题讨论】:

    标签: handlebars.js jira-plugin webresource


    【解决方案1】:

    我会这样做

    我将在一个一般可访问的 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];
    }
    

    这是假设我有适当引用的车把 js 库(类似于车把-v1.3.0.js)。

    然后在我需要模板显示的视图中,我将声明模板如下所示

    template: fnGetTemplate("backlog_coll");
    

    在我的视图的渲染函数中,我会调用这个模板来提供所需的数据,如下所示

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

    希望对你有帮助

    【讨论】:

    • 这个方法也找不到模板。它应该像/jira/s/ru_RU-g6ghuq/773/3/1.0-SNAPSHOT/_/download/resources/com.swan.agile.swan-agile:swan-agile-scrumboard/tpl/" + strName + ".handlebars
    • 实际上在示例中我将路径硬编码为“tpl”只是为了便于解释。实际上,您应该将其作为变量值取出,以便您可以动态填充该模板的路径,如下所示 function fnGetTemplate(strPath, strName) { if (Handlebars.templates === undefined || Handlebars.templates[ strName] === undefined) { $.ajax({ url : strPath + strName + ".handlebars",并将其称为模板:fnGetTemplate(strPath, strName);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-06
    • 1970-01-01
    • 2020-08-14
    • 2012-08-01
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多