2013-09-26 29 views
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 
    } 
}); 

是一種風格的首選?即與另一個相比,哪一個有任何主要缺點?

回答

4

通過在控制器定義之外定義輔助函數,可以使其成爲全局函數。這意味着該功能將在您的應用程序中隨處可用。如果這是一項要求,我會定義一個包含helperFn的單獨實用程序單例。

//in a separate file... 
Ext.define('App.Util', { 
    singleton: true, 

    helperFn: function() { 
     // 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 
     App.Util.helperFn(); 
    }, 
    onEvent2: function(args) { 
     // do other stuff 
     App.Util.helperFn(); 
    } 
}); 

通過在控制器的定義內部定義它,使其成爲控制器類的成員。這意味着它只能由控制器的實例調用。如果代碼是特定於控制器的,那麼這通常是優選的。

有第三個選項可用。如果你想要的功能以僅在控制器可用,但對別的(類似於Java中的私有方法)無法訪問,你可以將它設置這樣的:

Ext.define('App.controller.myController', (function() { 

    //now the function is only available below through closure 
    function helperFn() { 
     // do other, other stuff 
    } 

    return { 
     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 
     helperFn(); 
     }, 
     onEvent2: function (args) { 
     // do other stuff 
     helperFn(); 
     } 
    }; 
})()); 
+0

我明白了。是的,傳統的面向對象的樣式是我所要做的 - 感謝您指出全局事物,並感謝您花時間做出迴應 – Colin

+0

使用輔助函數定義Utility類是我所做的,我覺得它是一個非常優雅和DRY解決方案。 +1 –

+0

我會小心單身;我的經驗法則是任何需要了解其範圍的代碼都屬於具有該範圍的類。實用程序單例對於沒有副作用的真正簡單的函數是很好的,但我建議不要僅僅使用它們來在兩個類之間共享代碼。繼承和mixin對此很好。像上面那樣傳遞範圍是我書中的一大紅旗 - 從現在開始的六個月裏,你會發現自己摸不着頭腦,試圖琢磨從哪裏來的東西,而不是隻讀代碼。 –