javascript - mustache .js + jQuery : what is the minimal working example ?

标签 javascript jquery html mustache

我想在我的 HTML5 应用程序中使用 mustache.js 和 jQuery,但我无法让所有组件协同工作。每个文件都找到了,这里没有问题(模板加载正确,我可以在 Firebug 调试器中看到它的值)。

这是我的 index.html :

<!DOCTYPE html>
<html lang="fr">
    <head><meta charset="utf-8"></head>
    <body>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
        <script src="../js/jquery.mustache.js"></script>
        <script src="../js/app.js"></script>
        <div id="content"></div>
    </body>
</html>

这是我的 app.js 文件:

$(document).ready(function() {
    var template = $.get('../templates/article.mustache');
    $.getJSON('../js/article.json', function(view) {
        var html = Mustache.to_html(template, view);
        $("#content").append(html);
    });
});

jquery.mustache.js 文件是从 https://github.com/janl/mustache.js 生成的文件:

/*
Shameless port of a shameless port
@defunkt => @janl => @aq

See http://github.com/defunkt/mustache for more info.
*/

;(function($) {

// <snip> mustache.js code

  $.mustache = function(template, view, partials) {
    return Mustache.to_html(template, view, partials);
  };

})(jQuery);

显示注意。 Firebug 告诉我

Mustache is not defined

查看截图: enter image description here

我知道少了什么,但我说不出来是什么。

谢谢。


编辑: 最小 示例的正确和完整答案如下:

  • 在脚本中编写模板,不要从文件中加载
  • json 数据同上
  • 阅读 jQuery 的生成方式并使用 $.mustache.to_html 函数代替(在 github 上记录的)Mustache.to_html(感谢 @mikez302)
  • 重构直到放弃
$(document).ready(function() {
  var template = " ... {{title}} ... ";
  var json = {title: "titre article" }
  var article = $.mustache(template, json);
  $("#content").append(article);
});

但是,从另一个文件读取 json 很容易:

$(document).ready(function() {
  var template = " ... {{title}} ... ";
  $.getJSON('../js/article.json', function(view) {
    var article = $.mustache(template, view);
    $("#content").append(article);
  });
});

您终于可以从文件中读取模板了:

$(document).ready(function() {
  $.getJSON('../js/article.json', function(view) {
    $.get("../templates/article.mustache", function(template) {
      var article = $.mustache(template, view);
      $("#content").append(article);
    });
  });
});

工作示例(没有加载顺序问题):

$(document).ready(function() {
  $.getJSON('../js/article.json', function(model) { return onJSON(model); });
});

function onJSON(model) {
  $.get("../templates/article.mustache", function(view) {
    var article = $.mustache(view, model);
    $("#content").append(article);
  });
}

最佳答案

代替 Mustache.to_html,试试 $.mustache。在我看来,Mustache 变量是在函数内定义的,因此无法在函数外部直接访问。

关于javascript - mustache .js + jQuery : what is the minimal working example ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6439935/

相关文章:

javascript - 如何保存 Canvas 压缩

javascript - 如何将项目添加到 ng-repeat 列表并停止通过数据绑定(bind)更改列表项目

javascript - 在 div 中添加垂直线最实用的方法是什么?

jquery - 使用 jquery 函数将 var 附加到 url

html - 如何在图片的右上角放置按钮

html - 我们如何在没有像素化的情况下将小尺寸图像视为封面图像

动画半圆饼图上的 CSS Z-fighting

javascript - 如何基于当前路由/URL 构建动态面包屑组件?

javascript - 如何使用 jQuery UI 调整 datatables.js 列的大小

jquery - 更改 div 的内容 - jQuery