2012-05-06 28 views
1

在調解器模式的以下實現中,爲什麼initialize方法中的this.name始終未定義?因爲我期待它成爲TestObject。我怎樣才能做到這一點?JavaScript調解器模式;組件名稱未定義

另外我如何創建TestObject的新實例?

Mediator = function() { 

     var debug = function() { 
      // console.log or air.trace as desired 
     }; 

     var components = {}; 

     var broadcast = function(event, args, source) { 
      var e = event || false; 
      var a = args || []; 
      if (!e) { 
       return; 
      } 
      //debug(["Mediator received", e, a].join(' ')); 
      for (var c in components) { 
       if (typeof components[c]["on" + e] == "function") { 
        try { 
         //debug("Mediator calling " + e + " on " + c); 
         var s = source || components[c]; 
         components[c]["on" + e].apply(s, a); 
        } catch (err) { 
         debug(["Mediator error.", e, a, s, err].join(' ')); 
        } 
       } 
      } 
     }; 

     var addComponent = function(name, component, replaceDuplicate) { 
      if (name in components) { 
       if (replaceDuplicate) { 
        removeComponent(name); 
       } else { 
        throw new Error('Mediator name conflict: ' + name); 
       } 
      } 
      components[name] = component; 
     }; 

     var removeComponent = function(name) { 
      if (name in components) { 
       delete components[name]; 
      } 
     }; 

     var getComponent = function(name) { 
      return components[name] || false; 
     }; 

     var contains = function(name) { 
      return (name in components); 
     }; 

     return { 
      name  : "Mediator", 
      broadcast : broadcast, 
      add  : addComponent, 
      rem  : removeComponent, 
      get  : getComponent, 
      has  : contains 
     }; 
    }(); 


    // Components  
    Mediator.add('TestObject', function() { 

     var someNumber = 0; // sample variable 
     var someString = 'another sample variable'; 

     return { 
      onInitialize: function() { 
       // this.name is automatically assigned by the Mediator 
       alert(this.name + " initialized."); 
      }, 
      onFakeEvent: function() { 
       someNumber++; 
       alert("Handled " + someNumber + " times!"); 
      }, 
      onSetString: function(str) { 
       someString = str; 
       alert('Assigned ' + someString); 
      } 
     } 
    }()); 

    Mediator.broadcast("Initialize");     
    Mediator.broadcast('FakeEvent');     
    Mediator.broadcast('SetString', ['test string']); 
    Mediator.broadcast('FakeEvent');     
    Mediator.broadcast('SessionStart'); 

回答

2

這是因爲在你返回函數集團,this代表集團本身,而不是中介對象(你可以在onInitialize嘗試console.log(this);看到)。

編輯

爲了一個名字組件添加到您的回調,你可以做這樣的事情

var addComponent = function(varName, component, replaceDuplicate) { 
    if (varName in components) { 
     if (replaceDuplicate) { 
      removeComponent(varName); 
     } else { 
      throw new Error('Mediator name conflict: ' + varName); 
     } 
    } 
    components[varName] = component; 
    components[varName].name = varName; 
}; 

有很多的名字,所以我改變了當地namevarName

+0

我以爲this.name應該讓我「TestObject」? – XGreen

+1

「name」不是作爲回調函數的對象的值。事實上,名稱甚至不是Mediator的一個屬性,它僅僅是一個局部變量,用於傳輸將用於定義'components [name]'的字符串。請參閱編輯的答案以訪問名稱變量。 –

+0

對不起,你還介意幫助我創建另一個TestObject的實例? – XGreen