【问题标题】:when passing a variable into a backbone template, how to reference it in the template?将变量传递到主干模板时,如何在模板中引用它?
【发布时间】:2012-05-11 08:36:19
【问题描述】:

模板如下所示。

<div>
    <H5>Status for your request</H5>
    <table>
    <tbody>
    <tr>
        <th>RequestId</th>
        <th><%=id%></th>
    </tr>
    <tr>
        <th>Email</th>
        <th><%=emailId%></th>
    </tr>       
    <tr>
        <th>Status</th>
        <th><%=status%></th>
    </tr>       
     </tbody>
     </table>
</div>

这是呈现页面的 View Javascript。

window.StatusView = Backbone.View.extend({

initialize:function () {
    console.log('Initializing Status View');
    this.template = _.template(tpl.get('status'));
},

render:function (eventName) {

    $(this.el).html(this.template());
    return this;
},

events: { "click button#status-form-submit" : "getStatus" },

getStatus:function(){

    var requestId = $('input[name=requestId]').val();
    requestId= $.trim( requestId );

    var request  = requests.get( requestId );

    var statusTemplate = _.template(tpl.get('status-display'));
    var statusHtml = statusTemplate( request );
    $('#results-span').html( statusHtml );
}

});

当点击输入时,会读取 requestId 并将状态附加到 id 为 'results-span' 的 html 元素中。

将html-template中的值替换为变量值时失败。

var statusTemplate = _.template(tpl.get('status-display'));
var statusHtml = statusTemplate( request );

渲染失败并出现以下错误。

Uncaught ReferenceError: emailId is not defined
(anonymous function)
_.templateunderscore-1.3.1.js:931
window.StatusView.Backbone.View.extend.getStatusstatus.js:34
jQuery.event.dispatchjquery.js:3242
jQuery.event.add.elemData.handle.eventHandle

【问题讨论】:

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


    【解决方案1】:

    下划线的_.template

    将 JavaScript 模板编译为可评估以进行渲染的函数。
    [...]

    var compiled = _.template("hello: <%= name %>");
    compiled({name : 'moe'});
    => "hello: moe"
    

    因此,基本上,您将模板函数交给一个对象,然后模板在该对象内部查找您在模板中使用的值;如果你有这个:

    <%= property %>
    

    在您的模板中,您将模板函数称为t(data),然后模板函数将查找data.property

    通常您将视图的模型转换为 JSON 并将该对象交给模板:

    render: function (eventName) {
        $(this.el).html(this.template(this.model.toJSON()));
        return this;
    }
    

    我不知道你的eventName 是什么或者你打算用它做什么,但你需要得到一个具有这种结构的对象:

    data = { id: '...', emailId: '...', status: '...' }
    

    从某个地方把它交给模板函数:

    var html = this.template(data)
    

    获取一些 HTML 放在页面上。

    演示(带有用于说明目的的假模型):http://jsfiddle.net/ambiguous/hpSpf/

    【讨论】:

    • 感谢您的指点。我没有传递 Backbone 模型,而是传递了一个从模型创建的纯 javascript 对象。 var data = { id : request.get('id'), emailId : request.get('emailId'), status : request.get('status') }; 。现在模板正在渲染它。 BackBone 模型和纯 JS 对象有什么区别?
    • @Nambi:视图通常在 Backbone 中显示模型或集合,并且该模型或集合在 this.model or this.collection 中。然后使用 toJSON 方法将模型/集合转换为纯 JavaScript 数据结构。如果request 是一个主干模型,那么您的var data = { ... }var data = request.toJSON() 相同,只是toJSON 将包含所有request 属性。模板确实很关心,它只需要一个具有正确键的对象。
    【解决方案2】:
    OptionalExtrasView = Backbone.View.extend({
        initialize: function() {
            this.render();
        },
        render: function() {
            // Get the product id
            //var productid = $( this ).attr( "productid" );
            var data = {name : 'moe'};
    
            var tmpl = _.template($('#pddorder_optionalextras').html() );
            this.$el.html(tmpl(data));
        }
    });
    
       var search_view = new OptionalExtrasView({ el :     $('.pddorder_optionalextras_div')});
    

    就在body标签之前:

          <script type="text/template" id="pddorder_optionalextras">
       <%= name %>
        </script> 
    

    【讨论】:

    • 我使用 Node.js 得到这个,错误消息是---->名称未定义
    • 我认为这是因为我应该使用 EJS 模板而不是下划线模板
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 2011-07-29
    • 2016-02-16
    • 1970-01-01
    • 2023-03-24
    • 2015-06-22
    相关资源
    最近更新 更多