小胡子模板:如何在骨干视图中引用外部文件

und*_*666 1 javascript mustache backbone.js

以下代码(*)有效,但我希望不使用:

$(this.el).html(Mustache.render("<h2>{{title}}</h2>", view));
Run Code Online (Sandbox Code Playgroud)

我想要做:

$(this.el).html(Mustache.render("somePath/myFile.html", view));
Run Code Online (Sandbox Code Playgroud)

我该怎么做?

(*)

render: function () 
{
    var view = {
        response: this.model.title
    };
    $(this.el).html(Mustache.render("<h2>{{{title}}}</h2>", view)); // it works
    $(this.el).html(Mustache.render("myFile.html", view)); // it does not work
},
Run Code Online (Sandbox Code Playgroud)

dri*_*hev 5

你可以做 :

$.get("myFile.html", function(html) { $(this.el).html(Mustache.render(html, view)) });
Run Code Online (Sandbox Code Playgroud)

$.get是对该文件的简写AJAX请求,然后使用file(html)的内容作为要呈现的Mustache的HTML.