2017-09-11 56 views
0

我有幾個異步獲取的屬性。我想將這些屬性的結果綁定到我的應用程序中的視圖。但是,get屬性將視圖模型的變量視爲null。結果到達後我如何將它們綁定?在下面的代碼中,userEmailappVersion在它們各自的get函數中爲null/undefined。如何在打字稿中設置可觀察屬性異步?

export class AccountViewModel extends Observable { 
    private _userEmail : Email; 
    private _appVersion : string; 

    constructor() { 
     super(); 

     appVersion.getVersionName().then(
      (result) => 
      { 
       this._appVersion = result; 
       console.log(this._appVersion); 
      }); 

     firebase.getCurrentUser().then(
      (result) => { 
       this._userEmail = new Email(result.email); 
      }); 
    } 

    get userEmail(): string { 
     return this._userEmail != null ? this._userEmail.Value : ""; 
    } 

    get appVersion(): string { 
     return this._appVersion; 
    } 

    public logout() { 
     firebase.logout().then((result) => { 
      Navigator.navigateTo({ 
       moduleName : "views/register/register", 
       backstackVisible : false, 
       clearHistory : true 
      }) 
     }).catch((error) => { 
      dialogs.alert({ 
       title: "Error", 
       message: "A system error occurred trying to logout.", 
       okButtonText: "Ok" 
      }); 
     }); 
    } 
} 
+0

我不知道引擎蓋下,但如何運作nativescript如果自動提高你變動事件,你可能必須定義制定者它可以掛鉤進去。你正在改變支持價值,但你可能繞過了很多內建機制。這或者你可能不得不手動提高通知事件。 –

回答

0

可觀察到的類有一個notifyPropertyChange方法:)

export class AccountViewModel extends Observable { 
    private _appVersion : string; 

    constructor() { 
     super(); 

     appVersion.getVersionName().then((result) => { 
      this._appVersion = result; 
      this.notifyPropertyChange("appVersion", this._appVersion); 
     }); 
    } 

    get appVersion() : string { 
     return this._appVersion; 
    } 

    public logout() { 
     firebase.logout().then((result) => { 
      Navigator.navigateTo({ 
       moduleName : "views/register/register", 
       backstackVisible : false, 
       clearHistory : true 
      }) 
     }).catch((error) => { 
      dialogs.alert({ 
       title: "Error", 
       message: "A system error occurred trying to logout.", 
       okButtonText: "Ok" 
      }); 
     }); 
    } 
}