Best practices for Storing JSON in DOM

后端 未结 4 1063
刺人心
刺人心 2021-01-05 05:34

I want to render some json data using HTML template.

I haven\'t started implementing anything yet, but I would like to be able to \"set\" values of data from json t

4条回答
  •  Happy的楠姐
    2021-01-05 06:06

    So you want to keep a reference to the JSON data that created your DOMFragment from a template?

    Let's say you have a template function that takes a template and data and returns a DOM node.

    var node = template(tmpl, json);
    node.dataset.origJson = json;
    node.dataset.templateName = tmpl.name;
    

    You can store the original json on the dataset of a node. You may need a dataset shim though.

    There is also no way to "map" JSON to HTML without using a template engine. Even then you would have to store the template name in the json data (as meta data) and that feels ugly to me.

提交回复
热议问题