i modelli oggetto JSON può apparire come segue:
{
"templates": {"name": "optionTemplate",
"template": "{{#options}}<option value='{{value}}'>{{display}}</option>{{/options}}"
}
}
Questo definirà un modello per le opzioni in una casella di selezione.
È possibile aggiungere il modello utilizzando il codice specificato (in realtà ho ottimizzato leggermente come non ho potuto farlo funzionare come specificato):
$.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);
}
Per chiarezza, ecco cosa contiene options.json :
{
"options": [
{ "value": "optionValue",
"display": "optionDisplay"
},
{ "value": "optionValue2",
"display": "optionDisplay2"
}]
}
Non vorrei sapere come si ottiene :)
fonte
2013-05-09 14:02:40