Dart JS Interop - 如何传递函数的回调对象

时间:2013-03-13 18:47:01

标签: dart

我正在使用Pase.com和js SDK作为我的Dart App的后端。除了解析sdk接受回调函数的对象之外,这个工作正常。在飞镖中我不确定如何做到这一点,我可以让单个回调工作正常。但我完全迷失在这里。

Normal Parse JS用于注册用户

var user = new Parse.User();
  user.set("username", "my name");
  user.set("password", "my pass");
  user.set("email", "email@example.com");

  // other fields can be set just like with Parse.Object
  user.set("phone", "415-392-0202");

  user.signUp(null, {
    success: function(user) {
      // Hooray! Let them use the app now.
    },
    error: function(user, error) {
      // Show the error message somewhere and let the user try again.
      alert("Error: " + error.code + " " + error.message);
    }
  });

我的飞镖代码

void registerSuccess(user) {
    print("success");
  }

  void registerFailed(user, error){
    print("fail");
  }

  void register(String email, String password)
  {
    js.scoped(() {

      var parse = js.context.Parse;


      var parseUser = new js.Proxy(parse.User);

      parseUser.set("username", "my name");
      parseUser.set("password", "my pass");
      parseUser.set("email", "email@example.com");

      print(parseUser.getEmail());

      var callbackSuccess = new js.Callback.once(() => registerSuccess());
      var callbackFailed = new js.Callback.once(() => registerFailed());

      parseUser.signUp(null,{"success":callbackSuccess, "error": callbackFailed});

      //parseUser.signUp();
    });
  }

此外,回调函数还需要从js传回的接受变量。

任何帮助都会受到赞赏,我已经开了2天了。

1 个答案:

答案 0 :(得分:3)

而不是:

var callbackSuccess = new js.Callback.once(() => registerSuccess());
var callbackFailed = new js.Callback.once(() => registerFailed());
parseUser.signUp(null,{"success":callbackSuccess, "error": callbackFailed});

使用:

var callbackSuccess = new js.Callback.once(registerSuccess);
var callbackFailed = new js.Callback.once(registerFailed);
parseUser.signUp(null, js.map({"success":callbackSuccess, "error": callbackFailed}));