如何将附加变量传递给下划线模板 [英] How to pass additional variables in to underscores templates

查看:16
本文介绍了如何将附加变量传递给下划线模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主干视图,它在下划线模板中呈现搜索结果.因为我想在结果中突出显示搜索词,所以模板中有以下打印方法:

I've a backbone view that renders a search result in a underscore template. As I want to highlight the search term in the result I have the following print method in the template:

print(someKey.replace(searchTerm, '<b>' + searchTerm + '</b>')

它按方面工作,但我必须在全局命名空间中设置 searchTerm 变量才能完成这项工作.我想知道是否有办法在打印方法中访问我的视图模型,所以我可以这样写:

It works as aspected, but I have to set the searchTerm variable in the global namespace to get this work. I wonder if there is a way to access my views model in the print method, so I could write it like this:

print(someKey.replace(searchTerm, '<b>' + this.model.get('searchTerm') + '</b>')

或者,如果我可以在渲染函数中将 searchTerm 设置为局部变量并在我的模板中访问它.

Or if I could set searchTerm as a local variable in my render function and access it in my template.

这是整个 Backbone 视图:

Here is the whole Backbone view:

var searchTerm;
var SearchResultView = Backbone.View.extend({
    initialize: function() {
        this.model.bind('change:rows', this.render, this);
        this.model.bind('change:searchTerm', this.clear, this)
    },
    template: _.template("<tr><td><% print(someKey.replace(searchTerm, '<b>' + searchTerm + '</b>')); %></td></tr>", this),
    render: function() {
       searchTerm = this.model.get('searchTerm')
        _.each(this.model.get('rows'), function(row) {
            this.el.append($(this.template(row.doc)));
        }, this)
    },
    clear: function(){
        this.el.empty();
    }
});

推荐答案

我不确定我是否完全理解您的问题.但是我按照这些方法做了一些事情,将多个对象传递到我的模板函数中.

I'm not sure that I fully understand your question. But I do something along these lines to pass multiple objects into my template function.

 $(this.el).html(_.template(html)($.extend({}, this.model.toJSON(), App.lists.toJSON())))

jquery 的扩展功能让我可以将两个对象合二为一.因此,在您的情况下,您可以在模板期间将searchTerm"与您的模型合并.

The jquery extend function lets me merge two objects into one. So in your case you could merge your "searchTerm" in with your model during templating.

_.template(html)($.extend({}, row.doc, {"searchTerm": searchTerm}))

您的另一个选择是将您的整个模型传递给您的模板函数并在模板中执行您的下划线迭代.如果您需要更多解释,请告诉我.

Your other option would be to pass your entire model into you template function and perform your underscore iteration in the template. Let me know if you need more explanation on that.

这是 jquery 扩展

如果您不使用 jquery,下划线还有 extend 方法

And underscore also has and extend method if you aren't using jquery

编辑

这只是举个例子来说明我的第二个建议.可能需要进行一些调整才能投入您的,但这就是想法.

This is just to give you an example of my second suggestion. Would probably need some tweaking to throw into yours, but this is the idea.

template: _.template("

    <% _(rows).each(function(row) { %>
        <tr><td><% print(someKey.replace(searchTerm, '<b>' + searchTerm + '</b>')); %></td></tr>
<% } %>

"),
render: function(){
    this.el.append(this.template(this.model.toJSON()));
}

这篇关于如何将附加变量传递给下划线模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆