2011-09-05 5 views
0

jquery, mouthache 및 sammy로 couchapp를 만들려고합니다. 이 여러 페이지가 될 것이며, 나는이처럼 생성됩니다 : 다음JSON을 사용한 콧수염이 함수를 실행하지 않습니다.

$(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('#/')}); 

과 :

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 내가 데이터를 저장 JSON 파일입니다. .ms 파일은 콧수염 템플릿입니다. 것은 내가 JSON에 기능을 넣어 시도 할 때 여기에 설명 된 것처럼 것입니다 : 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); 

기능은 실행되지 않습니다. 그래서 내가 뭘 잘못하고 있니? JSON에 데이터와 함수를 저장하는 것이 좋은 방법이며 어떤 대안이 있습니까? 감사합니다

답변

1

당신의 람다가 올바르게 선언되지 않았습니다. 귀하의 json은 콧수염이 실행될 익명의 함수를 반환해야합니다.

여기에 어떻게해야할까요?

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