javascript - 在下划线模板中使用函数

标签 javascript function templates backbone.js underscore.js

我正在尝试在 Underscore 模板中使用一个函数 as shown here但我收到一个错误:

Uncaught ReferenceError: getProperty is not defined

下面是我使用的代码

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>

View 助手对象如下所示:

var viewHelper = {
    getProperty:function(propertyName, object) {
        if(typeof(object) === "undefined"){
            object = this["attributes"];
        }
        for (i in object) {
            if (_.isObject(object[i])) {
                var currentObj = object[i];
                if(_.has(currentObj, propertyName)){
                    return currentObj[propertyName];
                }
                this.getProperty(propertyName, currentObj);
            }
        }
    }
}

最佳答案

你的问题是,一旦你进入renderthis.template:

var html = _.template($(this.template), data);

已经是编译好的模板函数:

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

_.template调用:

Compiles JavaScript templates into functions that can be evaluated for rendering.

所以你是这样说的:

_.template($(some_compiled_template_function).html(), data);
//           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

那是执行 $(function() { ... }) $() 的形式并且:

Binds a function to be executed when the DOM has finished loading.

这一点点的困惑让一切都变得一团糟,困惑无处不在。修复您使用模板的方式,事情将开始变得更有意义:

render: function() {
    //...
    var html = this.template(data);
    //...
}

您的 this.template 将是 render 中的函数,因此将其作为函数调用。

演示(为清楚起见进行了一些简化):http://jsfiddle.net/ambiguous/SgEzH/

关于javascript - 在下划线模板中使用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10401837/

相关文章:

c++ - 传递二维数组 C++

c++ - 从这里实例化错误

c++ - C2975 'N' 的无效模板参数,预期的编译时常量表达式

javascript - 按住启动屏幕和启动屏幕

javascript - 向 POST 请求发送成功响应

javascript - 使用外部加载的链接执行 JavaScript 函数

检查函数中的指针地址和值

c++ - 访问结构时出现段错误

javascript - Owl Carousel 不适用于 Rails

javascript - 不要重复自己