Mustache with JSON do not execute the function
I'm trying to create couchapp with jquery, mustache and sammy. There will be several pages and I will generate them like this:
$(function () {
var $db = $.couch.db('test');
var app = $.sammy(function () {
this.get('#/', function () {
render('json/index.json', 'mustache/index.ms');
});
this.get('#/login', function () {
render('json/login.json', 'mustache/login.ms');
});
this.get('#/register', function () {
render('json/register.json', 'mustache/register.ms');
});
this.put('#/post/register', register);
this.put('#/post/login', login);
});
app.run('#/')});
and then:
function render(xjson, xms) {
$.getJSON(xjson, function (data) {
$.get(xms, function (template) {
var html = Mustache.to_html(template, data);
$('#content').empty();
$('#content').append(html);
});
});}
.js are JSON files where I store the data. .ms file开发者_如何学JAVAs are mustache templates. Thing is that when I try to put a function to JSON like it's described here: https://github.com/janl/mustache.js
var view = {
title: "Joe",
calc: function() {
return 2 + 4;
}
}
var template = "{{title}} spends {{calc}}";
var html = Mustache.to_html(template, view);
The function will not execute. So, what I'm doing wrong? Is it a good idea to store data and functions in JSON and what alternatives do I have? Thanks
your lambda isnt declared correctly.. your json should return an anonymous function which mustache will execute.
here is how you should be doing it.
var view = {
title: "Joe",
calc: function() {
return function(text) {return (2 + 4)}
}
}
var template = "{{title}} spends {{#calc}} {{/calc}}";
var html = Mustache.to_html(template, view);
Live Demo
精彩评论