Мне нужна помощь в использовании переменной javascript, установленной динамически в качестве моей переменной шаблона усов. Вот в чем проблема. Мой JSON выглядит так
jsonData = {
"recipemodules" : {
"enchiladas" : [
{
"title" : "Chicken Enchiladas",
"thumbnailurl" : "http:www.google.com/image",
"recipelink" : "location to recipe",
"credit" : "some credit"
},
{
"title" : "Chicken Enchiladas",
"thumbnailurl" : "http:www.google.com/image",
"recipelink" : "location to recipe",
"credit" : "some credit"
}
],
"roastchicken" : [
{
"title" : "Chicken Enchiladas",
"thumbnailurl" : "http:www.google.com/image",
"recipelink" : "location to recipe",
"credit" : "some credit"
},
{
"title" : "Chicken Enchiladas",
"thumbnailurl" : "http:www.google.com/image",
"recipelink" : "location to recipe",
"credit" : "some credit"
}
],
"salmon" : [
{
"title" : "salmon ",
"thumbnailurl" : "http:www.google.com/image",
"recipelink" : "location to recipe",
"credit" : "some credit"
},
{
"title" : "Chicken Enchiladas",
"thumbnailurl" : "http:www.google.com/image",
"recipelink" : "location to recipe",
"credit" : "some credit"
}
]
}
};
Затем я делаю следующее... перехожу на веб-страницу... получаю URL-адрес и на основе этого URL-адреса устанавливаю переменную.
var theCurrentURL = window.location.href;
var finalJSONobject = "";
switch(theCurrentURL) {
case "www.google.com/something1":
finalJSONobject = "enchiladas";
break;
case "www.google.com/something2":
finalJSONobject = "roastchicken";
break;
case "www.google.com/something3":
finalJSONobject = "salmon";
break;
default:
}
Наконец... вот как выглядит мой шаблон с усами
// create template
template = '{{#enchiladas}}<div class="mri-container"><img src="{{thumbnailurl}}" /></div>{{/enchiladas}}';
// parse template
Mustache.parse(template);
// render template
rendered = Mustache.render(template, jsonData.recipemodules);
// inject template to DOM
carousel.html(rendered);
Теперь он работает нормально, когда я использую {{#enchiladas}}{{/enchiladas}} ... но это не то, что мне нужно. я хочу иметь возможность использовать имя переменной вместо enchiladas.
если вы посмотрите на мой оператор switch... он возвращает finalJSONobject в виде строки. я хочу, чтобы мой шаблон выглядел как
template = '{{#finalJSONobject}}<div class="mri-container"><img src="{{thumbnailurl}}" /></div>{{/finalJSONobject}}';
Но это не работает. Шаблон усов использует jsonData.recipemodules в качестве своих данных. Поэтому jsonData.recipemodules.enchiladas будет работать... но я хочу динамически устанавливать "enchiladas", который установлен в моем операторе switch. Я хочу использовать мою динамически устанавливаемую переменную в шаблоне. я хочу использовать jsonData.recipemodules.finalJSONobject, чтобы на основе страницы... я просматривал данные, которые мне нужны.
Пожалуйста помогите! Спасибо большое.
Константин