我试图找出一种方法来保持我的私有函数和辅助方法真正私有。每个对象只应公开允许在外部调用的内容(激进,我知道!)。我很难通过以下方式使用 Backbone 视图执行此操作:
- 不牺牲可读性
- 不涉及很多样板
- 不会产生任何意想不到的后果
这是我的一般视图结构:
(function(){
//Private function no other view needs to care about
var addStuffToMyDom = function(model){
var newView = new Subview({model: model});
//Problem: this doesn't refer to the 'view' here
this.$el.append(newView.render().$el);
}
//Another trivial function which should really be private
var doSomeThingTrivial = function(){
this.$el.addClass("meh");
}
return BaseView.extend({
events: {
"click": doSomeThingTrivial
},
render: function(){
var me = this;
this.collection.each(addStuffToMyDom);
return this;
}
});
}());
如您所见,私有函数不能引用“this”来附加自己。
解决方案1:
(function(){
var me;
...
return BaseView.extend({
initialize: function(){
me = this;
}
});
}());
这有很多微妙的副作用+每次都必须这样做会很烦人。
解决方案2:
(function(){
var me;
...
return BaseView.extend({
events{
"click" : function(){
doSomeThingTrivial.call(this);
}
}
});
}());
这行得通,但是对于杂乱的代码来说,这是很多样板。
解决方案3:
(function(){
return BaseView.extend({
events: {..}
initialize: function(){
_.bindAll(this, this.events);
}
});
}());
我最喜欢这个;这很有效,可读性很强,并且可以像宣传的那样工作,但同样,对于每个视图来说,这是一个额外的步骤。我还缺少其他解决方案吗?