2012-10-29 19 views
0

我希望在_Templated對象出現問題時能夠發佈特定主題。 現在,我簡單地創建小部件的額外位混合:使用Dojo發佈特定窗口小部件方法中的主題

[...] 
return declare('hotplate.hotDojoAuth.LoginForm', [_WidgetBase, _TemplatedHooksMixin, _TemplatedMixin, _WidgetsInTemplateMixin ], { 

_TemplatedHooksMixin簡單地發出:

define([ 
    'dojo/_base/declare', 
    'dojo/_base/lang', 
    'dojo/topic', 

    ], function(
    declare 
    , lang 
    , topic 

){ 
    return declare(null, { 

     templatedHooks: true, 

     constructor: function(){ 
     this.templatedHooks = true; 
     topic.publish('hotplate/hotHooks/constructor', this); 
     }, 

     buildRendering: function(){ 
     topic.publish('hotplate/hotHooks/buildRendering/before', this); 
     this.inherited(arguments); 
     topic.publish('hotplate/hotHooks/buildRendering/after', this); 
     }, 

     destroyRendering: function(){ 
     topic.publish('hotplate/hotHooks/destroyRendering/before', this); 
     this.inherited(arguments); 
     topic.publish('hotplate/hotHooks/destroyRendering/after', this); 
     }, 

     postCreate: function(){ 
     topic.publish('hotplate/hotHooks/postCreate/before', this); 
     this.inherited(arguments); 
     topic.publish('hotplate/hotHooks/postCreate/after', this); 
     }, 

     startup: function(){ 
     topic.publish('hotplate/hotHooks/startup/before', this); 
     this.inherited(arguments); 
     topic.publish('hotplate/hotHooks/startup/after', this); 
     }, 

     destroy: function(){ 
     topic.publish('hotplate/hotHooks/destroy/before', this); 
     this.inherited(arguments); 
     topic.publish('hotplate/hotHooks/destroy/after', this); 
     } 

    }); 
    } 
); 

問題:

1)代碼是重複的,主要是因爲它使用「this」,「arguments」,「inherited」,所有人都尖叫着「別跟我一起玩!」 (尤其是this.inherited)。 。有關使用簡單參數創建一個函數的任何提示?

2)這是否是一種半方式的做法?這個想法是允許與我的庫無關的其他小部件更改_Templated小部件的內容。

3)如果這是一條好路徑(評論?),你認爲我稱呼路徑的方式是理智的嗎?

謝謝!

Merc。

回答

1

我想你應該看看使用dojo/aspect

http://dojotoolkit.org/reference-guide/1.8/dojo/aspect.html

constructor: function(){ 
    this.templatedHooks = true; 

    var methodsToDecorate = ["buildRendering", "destroyRendering", ...]; 
    array.forEach(methodsToDecorate, function(methodName) { 
     aspect.before(this, methodName, function(deferred){ 
      topic.publish('hotplate/hotHooks/' + methodName + '/before', this); 
     }); 
     aspect.after(this, methodName, function(deferred){ 
      topic.publish('hotplate/hotHooks/' + methodName + '/after', this); 
     }); 
    }); 

    topic.publish('hotplate/hotHooks/constructor', this); 
}, 
+0

我會給這個一展身手。但是,我特別擔心在我的函數中this.inherited(參數),以及在aspect.before(this,methodName,function(deferred){「將會是methodName(this函數)而不是對象本身。 – Merc

相關問題