【问题标题】:Pulling icanhaz templates from a remote server从远程服务器中提取 icanhaz 模板
【发布时间】:2013-05-03 09:29:01
【问题描述】:

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

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

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

【问题讨论】:

    标签: html ajax jquery icanhaz.js


    【解决方案1】:

    您的模板 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"
                 }]
    }
    

    请告诉我你过得怎么样 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-02
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 2012-10-25
      相关资源
      最近更新 更多