从远程服务器中提取icanhaz模板

use*_*794 5 html ajax jquery icanhaz.js

icanhaz文档以此为例说明如何从远程服务器中提取ich模板.

$.getJSON('/myserver/templates.json', function (templates) {
    $.each(templates, function (template) {
         ich.addTemplate(template.name, template.template);
    });
});
Run Code Online (Sandbox Code Playgroud)

但是,文档并没有真正告诉您远程服务器上的文件必须是什么样子.有人有主意吗?

Sim*_*ock 3

您的模板 JSON 对象可能如下所示:

{
   "templates": {"name": "optionTemplate",
                 "template": "{{#options}}<option value='{{value}}'>{{display}}</option>{{/options}}"
                }
}
Run Code Online (Sandbox Code Playgroud)

这将为选择框中的选项定义一个模板。

您可以使用您指定的代码添加模板(实际上我稍微调整了它,因为我无法让它按指定的方式工作):

$.getJSON('templates.json', function (templates) {
    $.each(templates, function () {
        ich.addTemplate(this.name, this.template);
    });
});

//now call getJSON on your input data

$.getJSON('options.json', function (data) {
    var optionElements = ich.optionTemplate(data);
    $('#selectBox').append(optionElements);
}
Run Code Online (Sandbox Code Playgroud)

为了清楚起见,以下是 options.json 包含的内容:

{
  "options": [
             { "value": "optionValue",
               "display": "optionDisplay"
             },
             { "value": "optionValue2",
               "display": "optionDisplay2"
             }]
}
Run Code Online (Sandbox Code Playgroud)

请告诉我你的进展如何:)