2017-02-10 10 views
1

我有這樣的代碼在我的應用程序:觀察員:我應該使用傳遞給函數值,或者我可以用THIS.VALUE安全呢?

observers: [ 
    '_dataGenericObserver(userData.generic)', 
    ], 

    // If this.userData.generic.id is set, then the register tab is 
    // NOT visible. In this case, the selected tab mustn't be "register"... 
    _dataGenericObserver: function(){ 
    if (this.userData.generic.id){ 
     this.selected = 'login'; 
    } else { 
     if (this.defaultRegister) { 
     this.selected = 'register'; 
     } else { 
     this.selected = 'login'; 
     } 
    } 
    }, 

這安全嗎?或者我應該總是做到這一點,而不是:

observers: [ 
    '_dataGenericObserver(userData.generic)', 
    ], 

    // If this.userData.generic.id is set, then the register tab is 
    // NOT visible. In this case, the selected tab mustn't be "register"... 
    _dataGenericObserver: function(generic){ 
    if (generic.id){ 
     this.selected = 'login'; 
    } else { 
     if (this.defaultRegister) { 
     this.selected = 'register'; 
     } else { 
     this.selected = 'login'; 
     } 
    } 
    }, 

...? 我注意到在某些情況下在應用程序的其他部分,如果我有一個觀察員_someFunc(value.subvalue),然後有_someFunc: function(subValue),在功能subValue設置,而this.value.subValue不是。但是,我無法複製它 - 對不起。

問題:

  • 是否總是建議使用傳遞給函數的值,而不是this.*
  • 什麼時候可能發生,一個subValuethis.value.subValue設置爲一個函數參數,但不是?

回答

0

據我所知,你應該始終明確地聲明和使用要在一個觀察者來訪問,而不是使用this因爲你經歷過,而觀察者功能的myVar參數this.myVar可能未設置的依賴關係。

至少這是由聚合物開發商的官方response。(有可能是這方面更多一些issues

這適用於聚合物的1.x不知道這是聚合物2.X不同

+0

太棒了。謝謝。 – Merc

相關問題