mardi 5 juillet 2016

How to render mustache using : external json that have function as variable


I want to render mustache.js example. but I want to get data from external file. I copy this data to "mydata.json" :

{
  "beatles": [
    { "firstName": "John", "lastName": "Lennon" },
    { "firstName": "Paul", "lastName": "McCartney" },
    { "firstName": "George", "lastName": "Harrison" },
    { "firstName": "Ringo", "lastName": "Starr" }
  ],
  "name": function () {
    return this.firstName + " " + this.lastName;
  }
}

I copy template to "template.html" :

{{#beatles}}
* {{name}}
{{/beatles}}

and render this files by this code :

$.get('template.html', function(template) 
{
    $.get('mydata.json',function(datas)
    {
        var rendered = Mustache.render(template, datas);
        $('#content').html(rendered);
    }
}

these codes not working until I remove function(s) from data file.

  • I tried : different 'dataType' in JQuery ajax request.
  • I tried : get data as text and then jQuery.parseJSON that data.
  • I tried : regular and LAMBDA syntax in functions scripts.

but I cant solve this problem.


Aucun commentaire:

Enregistrer un commentaire