Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.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_Backbone.js_Closures_Underscore.js - Fatal编程技术网

Javascript 如何将其他变量传递到下划线模板

Javascript 如何将其他变量传递到下划线模板,javascript,backbone.js,closures,underscore.js,Javascript,Backbone.js,Closures,Underscore.js,我有一个主干视图,它以下划线模板呈现搜索结果。由于我想突出显示结果中的搜索词,因此模板中有以下打印方法: print(someKey.replace(searchTerm, '<b>' + searchTerm + '</b>') 或者,我可以在渲染函数中将searchTerm设置为局部变量,并在模板中访问它 以下是整个主干视图: var searchTerm; var SearchResultView = Backbone.View.extend({ init

我有一个主干视图,它以下划线模板呈现搜索结果。由于我想突出显示结果中的搜索词,因此模板中有以下打印方法:

print(someKey.replace(searchTerm, '<b>' + searchTerm + '</b>')
或者,我可以在渲染函数中将searchTerm设置为局部变量,并在模板中访问它

以下是整个主干视图:

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();
    }
});
var搜索项;
var SearchResultView=Backbone.View.extend({
初始化:函数(){
this.model.bind('change:rows',this.render,this);
this.model.bind('change:searchTerm',this.clear,this)
},
模板:u.template(“,this),
render:function(){
searchTerm=this.model.get('searchTerm')
_.each(this.model.get('rows')、函数(row){
this.el.append($(this.template(row.doc));
},本页)
},
清除:函数(){
this.el.empty();
}
});

我不确定我是否完全理解你的问题。但是我沿着这些路线做了一些事情,将多个对象传递到我的模板函数中

 $(this.el).html(_.template(html)($.extend({}, this.model.toJSON(), App.lists.toJSON())))
jquery扩展函数允许我将两个对象合并为一个对象。因此,在您的案例中,您可以在模板制作期间将“searchTerm”与您的模型合并

_.template(html)($.extend({}, row.doc, {"searchTerm": searchTerm}))
另一个选项是将整个模型传递到模板函数中,并在模板中执行下划线迭代。如果你需要更多的解释,请告诉我

编辑:

这里有一个链接

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

编辑:

这只是我第二个建议的一个例子。可能需要一些调整来融入你的,但这就是想法

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()));
}
模板:\模板(“
"),
render:function(){
this.el.append(this.template(this.model.toJSON());
}

将一个新对象合并到我的结果中似乎有点过火,但经过思考,这似乎是解决问题的唯一办法,除了使用全局变量或将模板内联到函数中并在其中声明searchTerm变量之外。我同意这是一种非常奇怪的情况。您基本上是从模型中提取“searchTerm”,迭代模型集合,然后将“searchTerm”重新绑定到集合中的每个项。您可以始终将整个模型绑定到模板,而不是将其拆分。然后将html包装到模板中的u2;.each()中。这只是偏好。祝你的项目好运:D
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()));
}