2016-08-18 54 views
3

我試圖使用https://pub.dartlang.org/packages/js中的js包創建auth0 javascript API的dart實現。將JavaScript API返回的JavaScript對象轉換爲DART類

所以我加載在index.html的https://cdn.auth0.com/js/lock/10.0.0/lock.min.js,我創建了以下auth0lock鏢類:

@JS() 
library auth0lock; 

import 'package:js/js.dart'; 

@JS('Auth0Lock') 
class Auth0LockJS { 
    external factory Auth0LockJS(String token, String domain, Object options); 
    external on(String event, Function func); 
    external show(); 
} 

class Auth0Lock { 
    Auth0LockJS auth0; 
    Auth0Lock(String token, String domain, Object options) { 
    this.auth0 = new Auth0LockJS(token,domain,options); 
    } 
    on(String event, Function func) { 
    this.auth0.on(event,allowInterop(func)); 
    } 
    show() { 
    this.auth0.show(); 
    } 
} 

@anonymous 
@JS() 
class AuthResult { 
external factory AuthResult({ 
    String accessToken, 
    String idToken, 
    String idTokenPayload, 
    String state 
}); 
    external String get accessToken; 
    external set accessToken(String v); 
    external String get idToken; 
    external set idToken(String v); 
    external Object get idTokenPayload; 
    external set idTokenPayload(Object v); 
    external String get state; 
    external set state(String v); 
} 

我正在寫一個angular2鏢應用程序,所以我創建的使用auth0lock.dart認證服務我之前使用以下代碼顯示的文件:

@Injectable() 
class Auth { 
    Auth0Lock lock; 

    authenticatedEvent(AuthResult authResult) { 
     print(authResult); 
    } 

    Auth() { 
     this.lock = new Auth0Lock(configObj.auth0.apiKey, configObj.auth0.domain,{}); 
     this.lock.on("authenticated", authenticatedEvent); 
    } 
    updateProfileName(data) { 
     var profile = data['profile'] != null ? data['profile'] : data; 
     print(profile['name']); 
    } 

    authenticated() { 
     return false; 
    } 

    login() { 
     this.lock.show(); 
    } 
} 

現在...一切實際上都有效!我在auth_service.dart文件中創建了一個名爲lock的變量,類型爲AuthLock。我的例子是帶參數,運行show()函數,連接到authenticated事件,並且在驗證完成時觸發該函數。

我創建了一個類AuthResult,但它忽略了它,我得到了一個從函數處理函數返回的javascript對象。

我使用Intellij編寫代碼並使用dartium執行。所以當我在auth_service.dart文件的print(authResult);行上放置一個斷點時,調試器顯示authResult對象包含Object類型的JavaScript ViewJSObjectimpl類型的classJavascript View包含此函數處理程序返回的所有屬性。

如何將它轉換爲飛鏢中的常規課程?顯然,我創建了AuthResult類錯誤,導致函數處理程序不返回此類型。所以我錯過了什麼?

謝謝!

更新

authenticatedEvent功能我試圖打印authResult.values,所產生的錯誤的類類型的JSObjectImpl不具有該功能的內部。所以返回對象的類型是JSObjectImpl。奇怪的是,我不知道在哪裏JSObjectImpl定義所以我刪除變量類型的authResult,並試圖用下面的代碼使用目錄裏面的變量:

authenticatedEvent(authResult) { 
    var a = authResult.accessToken; 
    print(a); 
} 

這個工程。爲什麼我無法在飛鏢中找到JSObjectImpl類型?任何快速的方法來將其轉換爲飛鏢課程?

感謝

回答

1

也許你僅僅需要從AuthResult

刪除@anonymous簡化,但完整的和類似的例子

的index.html

<!doctype html> 
<html> 
    <head> 
    <script> 
     var Apple = function(type) { 
     this.type = type; 
     this.color = "red"; 
     this.getInfo2 = function() { 
      return this.color + ' ' + this.type + ' apple'; 
     }; 
     }; 

     Apple.prototype.getInfo = function() { 
     return this.color + ' ' + this.type + ' apple'; 
     }; 

     function callback(f) { 
     console.log(f); 
     f(new Apple('Golden Delicious')); 
     } 
    </script> 
    </head> 
    <body> 
    <script type="application/dart" src="index.dart"></script> 
    <script src="packages/browser/dart.js"></script> 
    </body> 
</html> 

index.dart

@JS() 
library typed_callback.web; 

import 'package:js/js.dart'; 

@JS() 
class Apple { 
    external String get type; 
    external set type(String type); 
    external String get color; 
    external set color(String color); 
    external String getInfo(); 
    external String getInfo2(); 
    external Apple(String type); 
} 

typedef void Callback(Apple a); 

@JS('callback') 
external void callback(Callback /* or just Function */ f); 

f(Apple apple) { 
    print(apple.getInfo()); 
    print(apple); // prints the stringified JS object [object Object] 
} 

main() async { 
    callback(allowInterop(f)); 
} 
+0

嗨。謝謝你的幫助。不幸的是,刪除@anonymous標記不能解決問題。我得到的AuthResult是一個json對象而不是一個類。我想知道如何將它轉換成飛鏢班。 – ufk

+0

在我的示例中,傳遞給'Apple'的東西在打印時也看起來像一個JS對象,但可以像Dart類一樣使用。我不認爲有一種方法可以創建真正的Dart課程。所有你得到的是s代理,它允許你訪問像Dart類一樣的屬性和方法。 –

相關問題