Загрузите динамически созданный JSON в шаблон усов - PullRequest
0 голосов
/ 08 января 2019

Я пытаюсь загрузить JSON в свой шаблон усов. Дело в том, что у меня нет файла .json на моем сервере, но я создаю его из листа Google.

Я использовал tabletop.js, чтобы получить данные из листа Google, и это работает. У меня есть рабочий шаблон усов.

Это скрипт, который я использую. Не могли бы вы указать мне правильное направление?

var publicSpreadsheetUrl = 'https://docs.google.com/spreadsheets/d/1jr7C7ec9HIlgfEdSYVs-kL_6DMsR_65AiQ9_Yepi3T0/edit?usp=sharing';
var count = 0;

function init() {
    Tabletop.init({
        key: publicSpreadsheetUrl,
        callback: showInfo,
        simpleSheet: true
    })
}
window.addEventListener('DOMContentLoaded', init);


//this prints the data into the template but using a .json file in the server (which I will not have in production)
$(document).ready(function(){
    $.when($.ajax({url: "../mustache/receta.mst", dataType: 'text'}),$.ajax({url: "../mustache/data-BD.json"}))
    .done(function(template, data){
        Mustache.parse(template[0]);
        var rendered = Mustache.render(template[0], {receta: data[0].receta});
        $(".container").html(rendered);
    })
});

// this prints the data directly from the sheet using tabletop, but does not use the mustache template
function showInfo(data, tabletop) {
var div = document.getElementById('data'),
    html = "<h1>SHEET " + (++count) + "</h1>",
    prop, i;
for(i = 0; i < data.length; i++) {
  for(prop in data[i]) {
    html = html + "&nbsp;-&nbsp;" + data[i][prop];
  }
  html = html + "<hr><br>";
}
div.innerHTML = div.innerHTML + html;
}

Я также пытался использовать этот скрипт, который печатает шаблон усов, но я не могу найти, как заменить {name: "Luke"} моими данными из таблицы

function loadUser() {
  $.get('template.mst', function(template) {
    var rendered = Mustache.render(template, {name: "Luke"});
    $('#target').html(rendered);
  });
}
...