2013-05-09 2 views
5

icanhaz 문서는 이것을 원격 서버에서 템플릿을 가져 오는 방법의 예로 예로 사용합니다.원격 서버에서 icanhaz 템플릿을 가져 오기

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

그러나 설명서에는 실제로 원격 서버의 파일이 어떤 모습인지 알려주지 않습니다. 누구든지 아이디어가 있습니까?

답변

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" 
      }] 
} 

내가 당신을 얼마나 알려주나요 :)