Javascript Mustache.js对象#<;文件>;没有方法';搜索';

Javascript Mustache.js对象#<;文件>;没有方法';搜索';,javascript,template-engine,mustache,Javascript,Template Engine,Mustache,我使用mustache.js作为客户端模板解决方案,但我遇到了一个问题,我不知道为什么会发生这种情况: 首先,这是我的模板: <ul> {{#steps}} <li> <a href="{{url}}"> <div class="step_visual {{step_status}}"> <span class="step_description">

我使用mustache.js作为客户端模板解决方案,但我遇到了一个问题,我不知道为什么会发生这种情况:

首先,这是我的模板:

<ul>
    {{#steps}}
    <li>
        <a href="{{url}}">
            <div class="step_visual {{step_status}}">
                <span class="step_description">{{description}}</span>
            </div>
        </a>
    </li>
    {{/steps}}
</ul>
我试着运行这个脚本在我的应用程序中呈现出来

var content = Mustache.render(template, view);
$(target).html(content).hide().fadeIn();
其中
模板
=上述模板|
视图
=视图对象 而
target
是我希望更改内容的html元素

Coffuse的一点是,当我的模板看起来像

{{#steps}}
    {{url}}
    {{description}}
    {{step_status}}
{{/steps}}
或者如果我的模板没有任何可循环的内容

可能是什么问题?你认为这是胡子虫吗

编辑 正如我看到的代码工作

我调用函数的方式可能有问题吗

tmplReplaceContent : function(json, tmpl, target) {
    var regex = new RegExp("\{{[a-zA-Z\.\_]*\}}"),
    view = '';
    function setOutput(template) {
        var content = Mustache.render(template, view);
        $(target).html(content).hide().fadeIn();
    }
    function doJSON(json) {
        view = json;
        if(!regex.test(tmpl)){
            /* get mustache tmpl from the path */
            $.get(msi.vars.tmpl_url + tmpl + '.mustache', setOutput);
        } else {
            setOutput(tmpl);
        }
    }
    /* json -> check if object */
    if (typeof json === 'object') {
        doJSON(json);
    } else {
        /* getJSON from the path */
        $.getJSON(msi.vars.base_url + json, doJSON);
    }
}

我从未有过这种解脱的感觉,因为我自己解决了我的问题,首先感谢你一直关注我的评论并帮助我解决问题

如果我继续在console.logging上记录东西,我本可以更早地解决问题,但我停止了

我注意到的一件事是,
$.get
函数给了我
#
,这几乎是一个对象,我可以更早地推断出响应是一个对象,从我问题的标题可以看出,它显然返回了一个对象

那么为什么不添加
dataType
属性呢

function doJSON(data){
    dataSource = data;
    if (!regex.test(tpl)) {
        $.get(msi.vars.tmpl_url + tpl + '.mustache', render, 'text'); // see here
    } else {
        render(tpl);
    }
}

这最终解决了问题。

代码正常,错误在其他地方。是的,我也这么认为,我也在这里测试过:。我更新了问题,添加了一些可能是bug来源的代码。我会以不同的方式实现
tmplReplaceContent
功能,但对我来说它看起来不太可疑。用Chrome调试这可能是个好主意。将其设置为“暂停未捕获的异常”,并检查调用树和作用域变量以了解发生了什么。我尝试了它,但不知道如何检查它,我在作用域变量(本地)上发现了一个异常,看起来像
arguments:Array[2]0:“search”1:#文档长度:2 u proto_u:Array[0]get message:function(){[本机代码]}get stack:function(){[native code]}set message:function(){[native code]}set stack:function(){[native code]}type:“undefined_method”\uuuuuu proto_uuuu:Error
在做了几次测试之后,我发现这是关于mustache引擎的,当模板文件来自外部文件时,它会抛出一个错误。我找不到补救办法,我正在考虑将外部文件的内容保存到一个变量中,然后使用它。但我不知道怎么做。做得很好+1-您可以(也可能应该)在服务器端通过发送
*。mustache
文件作为
内容类型:text/plain
来解决这个问题。这样,您就不需要更改
.get()
请求。我还建立了一个示例性解决方案,它利用jQuery的
延迟
来解决同步两个异步任务(获取JSON和模板)的问题:。我的第一个怀疑是,你会有一个时间问题,但很快就很清楚,事实并非如此。也许你可以利用它/学点新东西。哦,哇!!你真的付出了一些努力,谢谢你!!!超级的!!我几乎不懂你给我的密码,也许是bec。我还不知道。我会把它添加到书签中,然后也尝试一下,这样我就可以理解了。谢谢@Salac延迟的概念非常有用,也不太难。如果你不把它用在这个项目上,下次你肯定会发现它很有用。
function doJSON(data){
    dataSource = data;
    if (!regex.test(tpl)) {
        $.get(msi.vars.tmpl_url + tpl + '.mustache', render, 'text'); // see here
    } else {
        render(tpl);
    }
}