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

时间:2013-05-09 01:08:04

标签: html ajax jquery icanhaz.js

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

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

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

1 个答案:

答案 0 :(得分:3)

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

{
   "templates": {"name": "optionTemplate",
                 "template": "{{#options}}<option value='{{value}}'>{{display}}</option>{{/options}}"
                }
}

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

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

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

为清楚起见,这里是options.json包含的内容:

{
  "options": [
             { "value": "optionValue",
               "display": "optionDisplay"
             },
             { "value": "optionValue2",
               "display": "optionDisplay2"
             }]
}

请告诉我你的表现:)