Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用下划线.js加载外部模板 //path=外部文件的位置 //scriptBlockId=外部脚本块的id() //fillId=渲染时要放置模板的位置 var App=Backbone.View.extend({ render:函数(路径、scriptBlockId、fillId){ $.ajax({ async:false, 数据类型:“html”, 方法:“GET”, url:path, 成功:功能(响应){ //不知道为什么我们必须先这样做,然后才能选择脚本块? var section=$('#main')。追加(响应); var templateString=$(section).find('#'+scriptBlockId).html(); var compiledTemplate=ux0.template(templateString); var temp=compiledTemplate(); $(fillId).html(temp); } }); } }); var app=新app(); render(window.siteRoot+'Scripts/_test1.tmpl.html','addformtemplate','#main');_Javascript_Backbone.js_Underscore.js - Fatal编程技术网

Javascript 用下划线.js加载外部模板 //path=外部文件的位置 //scriptBlockId=外部脚本块的id() //fillId=渲染时要放置模板的位置 var App=Backbone.View.extend({ render:函数(路径、scriptBlockId、fillId){ $.ajax({ async:false, 数据类型:“html”, 方法:“GET”, url:path, 成功:功能(响应){ //不知道为什么我们必须先这样做,然后才能选择脚本块? var section=$('#main')。追加(响应); var templateString=$(section).find('#'+scriptBlockId).html(); var compiledTemplate=ux0.template(templateString); var temp=compiledTemplate(); $(fillId).html(temp); } }); } }); var app=新app(); render(window.siteRoot+'Scripts/_test1.tmpl.html','addformtemplate','#main');

Javascript 用下划线.js加载外部模板 //path=外部文件的位置 //scriptBlockId=外部脚本块的id() //fillId=渲染时要放置模板的位置 var App=Backbone.View.extend({ render:函数(路径、scriptBlockId、fillId){ $.ajax({ async:false, 数据类型:“html”, 方法:“GET”, url:path, 成功:功能(响应){ //不知道为什么我们必须先这样做,然后才能选择脚本块? var section=$('#main')。追加(响应); var templateString=$(section).find('#'+scriptBlockId).html(); var compiledTemplate=ux0.template(templateString); var temp=compiledTemplate(); $(fillId).html(temp); } }); } }); var app=新app(); render(window.siteRoot+'Scripts/_test1.tmpl.html','addformtemplate','#main');,javascript,backbone.js,underscore.js,Javascript,Backbone.js,Underscore.js,这个代码有效!为什么我们必须先追加我不知道…您可能遇到了一个问题,aysnc$.ajax //path = the location of the external file //scriptBlockId = the id of the external script block (<script id="add-form-tempate" type="text/html-template">) //fillId = where you want to place the temp

这个代码有效!为什么我们必须先追加我不知道…

您可能遇到了一个问题,aysnc
$.ajax

//path = the location of the external file
//scriptBlockId = the id of the external script block (<script id="add-form-tempate" type="text/html-template">)
//fillId = where you want to place the template when rendered
var App = Backbone.View.extend({
render: function (path, scriptBlockId, fillId) {

    $.ajax({
        async: false,
        dataType: 'html',
        method: 'GET',
        url: path,
        success: function (response) {
            //Not sure why we have to do this first, before we can select the script block?
            var section = $('#main').append(response);

            var templateString = $(section).find('#' + scriptBlockId).html();
            var compiledTemplate = _.template(templateString);
            var temp = compiledTemplate();

            $(fillId).html(temp);
        }
    });

}
});

var app = new App();
app.render(window.siteRoot + 'Scripts/_test1.tmpl.html', 'add-form-template', '#main');

我还尝试通过搜索HTML结果中的id并从脚本块中提取内容来完成我认为您正在尝试完成的任务。

这看起来很接近,但抛出了错误:TypeError:$(…)。find(…)[0]是未定义的。对于我来说,我不明白为什么这不起作用?它可以带回外部文件刚刚好,但我不能得到代码块@马克把它附加到
正文中
也许是第一个?@asawyer这行得通,我编辑了上面的代码,现在就可以了。我真的不明白为什么我们要先附加…@Mark尝试对字符串片段进行parseHtml调用,而不是附加到正文。
var App = Backbone.View.extend({
    render: function(path, scriptBlockId, fillId) {
        var self = this;

        $.ajax({
            async: false,
            dataType: 'html',
            method: 'GET',
            url: path,
            success: function(response) {
                var templateString = $(response).find(scriptBlockId)[0].innerHTML,
                    compildeTemplate = _.template(templateString),
                    temp = compildeTemplate();

                $(fillId).html(temp);
            }
        });

        return this;
    }
});

var app = new App();
app.render(window.siteRoot + 'Scripts/_test1.tmpl.html', 'add-form-tempate', '#main');