Mustache.js - 接受输入作为变量,但不接受 ajax 获取的输入:如何导入或排除故障

发布于 2024-09-11 08:07:16 字数 1346 浏览 0 评论 0原文

以此为例:http://mustache.github.com/#demo,模板并且 json 可以在功能上移动到 var:

template = '<h1>{{header}}</h1>{{#items}}{{#first}}<li><strong>{{name}}</strong></li> {{/first}}{{#link}} <li><a href="{{url}}">{{name}}</a></li> {{/link}}{{/items}}{{#empty}} <p>The list is empty.</p>{{/empty}}';

这就是事情发生的地方:

$.ajax({
    url: "must_template.js",
    dataType: "html",
    success: function(data) {
    template = data;
    }
});

Chrome

$.ajax({
url: "theJson2.txt",
dataType: "json",
success: function(data) {
        json = data;
        //json = $.parseJSON(data);
    }
});

的控制台显示与 var 完全相同的内容,但是 Mustache 在输入上中断,因为它们在输入上是“未定义”:

Uncaught TypeError: Cannot call method 'indexOf' of Undefined

我尝试更改dataType,使用 $.parseJSON 解析它,并且从外部文件导入模板或 JSON 时没有任何效果。

如果有任何解决 javascript 对象故障的提示,我们也将不胜感激。

更新: 代码在这里:http://dev.plitto.com/sandbox/mustache_riff/index4。 html theJson.txt 是 JSON。它被正确拉动。 console.log(data.header) 正确返回。 Must_template.js 是 Mustache 模板文件(从外部拉取将允许不同的主题)。

Using this as the example: http://mustache.github.com/#demo, the template and json can functionally be moved to a var:

template = '<h1>{{header}}</h1>{{#items}}{{#first}}<li><strong>{{name}}</strong></li> {{/first}}{{#link}} <li><a href="{{url}}">{{name}}</a></li> {{/link}}{{/items}}{{#empty}} <p>The list is empty.</p>{{/empty}}';

This is where things break down:

$.ajax({
    url: "must_template.js",
    dataType: "html",
    success: function(data) {
    template = data;
    }
});

and

$.ajax({
url: "theJson2.txt",
dataType: "json",
success: function(data) {
        json = data;
        //json = $.parseJSON(data);
    }
});

Chrome's console shows exactly the same content as the var, but Mustache breaks on the inputs because they're "Undefined" on input:

Uncaught TypeError: Cannot call method 'indexOf' of Undefined

I've tried changing the dataType, parsing it using $.parseJSON and nothing works when importing either the template or JSON from an external file.

If there are any tips for troubleshooting javascript objects, that would be appreciated as well.

Update:
Code is here: http://dev.plitto.com/sandbox/mustache_riff/index4.html
theJson.txt is the JSON. It's being pulled correctly. console.log(data.header) correctly returns.
must_template.js is the Mustache template file (pulling externally will allow for different themes).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魔法唧唧 2024-09-18 08:07:20

有关 Mustache / jQuery 的工作示例,请参阅
http://blog.xoundboy.com/?p=535

For a working example of Mustache / jQuery see
http://blog.xoundboy.com/?p=535

是你 2024-09-18 08:07:19

事实证明这是一个范围问题。

模板变量在 $.ajax() 调用之前定义,并且该值仅在 $.ajax 调用内有效。

Turns out this is a scope problem.

The template variable is defined before the $.ajax() call, and the value is only valid within the $.ajax call.

拥抱我好吗 2024-09-18 08:07:18

两者有什么区别? Json2.txt 和 Must_template.js 中有什么?

解析 JSON:

data = JSON.parse(data);

要查看对象中的内容,请使用 Firebug 或 Safari / Chrome 中的 JS 控制台:

console.log(data);

如果您获得更多信息,我们可以为您提供更多帮助:)

What is the difference between the two? And what are in theJson2.txt and must_template.js?

Parse JSON with:

data = JSON.parse(data);

And to see what's in the object, use Firebug or the JS Console in Safari / Chrome with:

console.log(data);

If you get some more information up we can be more help :)

橘虞初梦 2024-09-18 08:07:17

AJAX 请求是异步请求。如果您在 ajax 函数调用之外执行了 Mustache to_html 那么它将无法工作。因为它会在AJAX请求之前执行。

尝试以下操作。

Must_template.html 文件将包含,

<h1>{{header}}</h1>
{{#items}}{{#first}}
<li><strong>{{name}}</strong></li> 
{{/first}}{{#link}} <li><a href="{{url}}">{{name}}</a></li> {{/link}}{{/items}}   
{{#empty}} <p>The list is empty.</p>{{/empty}}

并且您的代码将是

$.ajax({
       url: "must_template.html",
       dataType: "html",
       success: function(data) {
           var template = data;

           $.ajax({
                      url: "theJson2.txt",
                      dataType: "json",
                      success: function(data2) {
                          var json = data2;
                          var html = Mustache.to_html(template, json);
                          $("#yourtargettag").html(html);                              
                      }
                  });
       }
   });

AJAX requests are async requests. if you executed mustache to_html outside the ajax function call then it wont work. Because it will be executed before AJAX request.

Try the following.

must_template.html file will contain,

<h1>{{header}}</h1>
{{#items}}{{#first}}
<li><strong>{{name}}</strong></li> 
{{/first}}{{#link}} <li><a href="{{url}}">{{name}}</a></li> {{/link}}{{/items}}   
{{#empty}} <p>The list is empty.</p>{{/empty}}

And your code will be

$.ajax({
       url: "must_template.html",
       dataType: "html",
       success: function(data) {
           var template = data;

           $.ajax({
                      url: "theJson2.txt",
                      dataType: "json",
                      success: function(data2) {
                          var json = data2;
                          var html = Mustache.to_html(template, json);
                          $("#yourtargettag").html(html);                              
                      }
                  });
       }
   });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文