【问题标题】:Using functions in a underscore template在下划线模板中使用函数
【发布时间】:2012-05-11 05:22:57
【问题描述】:

我正在尝试在下划线模板 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>

视图助手对象如下所示:

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);
            }
        }
    }
}

【问题讨论】:

  • 请发布viewHelper 对象的代码,并在您致电_.template 之前向我们展示data 的确切外观。
  • 您好,我已将代码添加到上述问题中,请检查

标签: javascript function templates backbone.js underscore.js


【解决方案1】:

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

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

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

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

_.template 电话:

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

所以你是这么说的:

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

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

绑定一个在 DOM 完成加载时执行的函数。

那一点点混乱使一切变得混乱,混乱比比皆是。修正你使用模板的方式,事情就会开始变得更有意义:

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

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

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

【讨论】:

    【解决方案2】:

    根据你参考的博客文章,

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多