Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.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 在下划线模板中使用函数_Javascript_Function_Templates_Backbone.js_Underscore.js - Fatal编程技术网

Javascript 在下划线模板中使用函数

Javascript 在下划线模板中使用函数,javascript,function,templates,backbone.js,underscore.js,Javascript,Function,Templates,Backbone.js,Underscore.js,我试图在下划线模板中使用函数,但出现错误: 未捕获引用错误:未定义getProperty 下面是我正在使用的代码 var CustomerDetailView = Backbone.View.extend({ tagName : "div", template: "#customer-detail-view-template", initialize: function() { _.bindAll(this, 'render'); thi

我试图在下划线模板中使用函数,但出现错误:

未捕获引用错误:未定义getProperty

下面是我正在使用的代码

var CustomerDetailView = Backbone.View.extend({
    tagName : "div",
    template: "#customer-detail-view-template",

    initialize: function() {
        _.bindAll(this, 'render');
        this.model.bind('change', this.render);
        this.initializeTemplate();
    },

    initializeTemplate: function() {
        this.template = _.template($(this.template).html());
    },

    render: function() {
        var data = this.model.toJSON();
        _.extend(data, viewHelper);
        console.log(data);
        var html = _.template($(this.template), data);
        $(this.el).html(html);
        return this;
    }
})
以及模板:

 <script type="text/template" id="customer-detail-view-template">
    <div style="height: 70px;">
        <span class="displayText">
            <p class="searchResultsAuxInfo">Full Name : <%= getProperty("full_name", null) %> </p>
            <p class="searchResultsAuxInfo">Country Code : <%= getProperty("country_code", null) %></p>
            <p class="searchResultsAuxInfo">street : <%= getProperty("street", null) %></p>
            <p class="searchResultsAuxInfo">maiden_name : <%= getProperty("maiden_name", null) %></p>
        </span>
        <span class="displayTextRight">
            <p class="searchResultsAuxInfo">marital_status_code : <%= getProperty("marital_status_code", null) %></p>
            <p class="searchResultsAuxInfo">tax_id_number : <%= getProperty("tax_id_number", null) %></p>
            <p class="searchResultsAuxInfo">primary_phone : <%= getProperty("primary_phone", null) %></p>
            <p class="searchResultsAuxInfo">customer_number : <%= getProperty("customer_number", null) %></p>
        </span>
    </div>
</script>

您的问题是,一旦进入
渲染
此.模板

var html = _.template($(this.template), data);
已编译的模板函数:

initializeTemplate: function() {
    this.template = _.template($(this.template).html());
}
电话:

将JavaScript模板编译成可以进行渲染评估的函数

你是这么说的:

_.template($(some_compiled_template_function).html(), data);
//           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
这是执行
$()
的形式,并且:

绑定DOM完成加载后要执行的函数

这一点点的混乱使一切都变得混乱,到处都是混乱。修正你如何使用模板,事情将开始变得更有意义:

render: function() {
    //...
    var html = this.template(data);
    //...
}
您的
this.template
将是
render
中的一个函数,因此将其作为函数调用


演示(为了清晰起见做了一些简化):

根据您提到的博客文章


只要删除这一行就可以了:“this.initializeTemplate();”

请发布
viewHelper
对象的代码,并在调用
模板之前向我们显示
数据的确切外观。您好,我已将代码添加到上述问题中,请检查
render: function() {
    //...
    var html = this.template(data);
    //...
}