Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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
Backbone.js 如何导入包含下划线模板的文件?_Backbone.js_Underscore.js - Fatal编程技术网

Backbone.js 如何导入包含下划线模板的文件?

Backbone.js 如何导入包含下划线模板的文件?,backbone.js,underscore.js,Backbone.js,Underscore.js,正如在任何项目中预期的那样,我的主HTML文件的90%由许多不同的模板组成,如下所示: <script type="text/template" id="template-parametros"> <div id="parametros"> <h2>Parâmetros</h2> <table id="tab-parametros"> <tr><td>Descrição</t

正如在任何项目中预期的那样,我的主HTML文件的90%由许多不同的模板组成,如下所示:

<script type="text/template" id="template-parametros">
  <div id="parametros">
    <h2>Parâmetros</h2>
    <table id="tab-parametros">
      <tr><td>Descrição</td><td>Tipo</td><td>Locale</td></tr>
    </table>
    <button id="parametros-ad">Adicionar</button>
  </div>
</script>

平底船
描述政治
阿迪西奥纳
我想把它们放在其他地方,这样用户体验人员就可以自己处理它们了。将它们放入另一个文件很容易,但以后如何导入它们?我已经尝试过了,但是浏览器尝试过,当然,失败了,无法将其解释为javascript代码。type=“text”也会失败。有什么想法吗

谢谢大家!

我使用一个模块加载器(requireJS),它有一个文本插件。它允许您将模板文件定义为参数,并在主干视图中使用

带有require的主干视图如下所示

define([
    'jquery',
    'underscore',
    'backbone',
    'text!/templates/templateFileName.html'  // Define the template using text! plugin
], function($, _, Backbone, myTemplate) {  // Include the template as an argument
    "use strict";

    ViewName = Backbone.View.extend({
        template: _.template(myTemplate),  // Setup my template (underscore)
        events: {
            ...
        },
        initialize: function() {
            ...     
        },
        render: function() {
            $(this.el).html(this.template()); // render the template
            return this;
        }
    });

    return ViewName;
});
要添加到这一点,使用下划线的
。.template()
很容易插值

假设我的templateFileName.html如下所示

<section>
    <div>
        <%= name %>
    </div>
    <div>
        <%= age %>
    </div>
</section>
那么:

如果(!async)async=false;
$.ajax({
异步:异步,
url:'/template/'+templateId,
数据类型:“json”,
成功:功能(响应){
$('body').append(response.content);
}
});
重复?||
var myHash = {"name":"Felix", "age":"9"};

$(this.el).html(this.template(myHash));