我正在尝试缓存(使用 jQuery 选择器) View 中的一些子元素。
var theView = Backbone.View.extend({
initialize:function(){
this.$subEl = this.$el.find('.sub-el-class');
},
template: Handlebars.compile($('#view-template').html()),
render: function(){
this.$el.html(this.template(this.model.toJSON()));
this.$subEl.hide();
return this;
}
});
当我渲染 View 时,$subEl (.sub-el-class) 不会隐藏。我不想每次必须对 .sub-el-class 元素执行某些操作时都使用 jQuery 选择器。
有什么想法不适用于我的代码吗?
最佳答案
this.$subEl = this.$el.find('.sub-el-class');
这是一个指向 .sub-el-class 的指针,您可以在初始化 View 时设置它。
每次调用 this.$subEl 时都已准备好一个 jQuery 对象,因此它不会再次搜索 DOM。
关于jquery - 在主干中缓存 View 的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15022422/