0
我使用ExtJS 4並遵守其MVC模式。我有點困惑於把事件處理程序使用的幫助函數放在哪裏。這是目前我在做什麼:ExtJS:控制器功能放置
Ext.define('App.controller.myController, {
extend: 'Ext.app.Controller.
stores: [...],
models: [...],
views: [...],
init: function() {
this.control({
'selector' : {
event1: this.onEvent1,
event2: this.onEvent2
}
});
},
onEvent1: function(args) {
// do stuff
helperFn();
},
onEvent2: function(args) {
// do other stuff
helperFn();
}
});
// is this 'right'?
function helperFn() {
// do other, other stuff
}
這是正確的設置?或者我應該這樣做:
Ext.define('App.controller.myController, {
extend: 'Ext.app.Controller.
stores: [...],
models: [...],
views: [...],
init: function() {
this.control({
'selector' : {
event1: this.onEvent1.bind(this),
event2: this.onEvent2.bind(this)
}
});
},
onEvent1: function(args) {
// do stuff
this.helperFn();
},
onEvent2: function(args) {
// do other stuff
this.helperFn();
},
helperFn(): function() {
// do other, other stuff
}
});
是一種風格的首選?即與另一個相比,哪一個有任何主要缺點?
我明白了。是的,傳統的面向對象的樣式是我所要做的 - 感謝您指出全局事物,並感謝您花時間做出迴應 – Colin
使用輔助函數定義Utility類是我所做的,我覺得它是一個非常優雅和DRY解決方案。 +1 –
我會小心單身;我的經驗法則是任何需要了解其範圍的代碼都屬於具有該範圍的類。實用程序單例對於沒有副作用的真正簡單的函數是很好的,但我建議不要僅僅使用它們來在兩個類之間共享代碼。繼承和mixin對此很好。像上面那樣傳遞範圍是我書中的一大紅旗 - 從現在開始的六個月裏,你會發現自己摸不着頭腦,試圖琢磨從哪裏來的東西,而不是隻讀代碼。 –