1

我正在尝试在 scalajs-react 中成功提交另一个弹出窗口时打开一个弹出窗口。我的问题是成功状态没有得到修改。这是我的 addNewAgent 方法,在提交第一个弹出窗口时作为回调调用。

def addNewAgent(userModel: UserModel, addNewAgent: Boolean = false): Callback = {
  println(addNewAgent)
  if(addNewAgent){
    createUser(userModel).onComplete {
      case Success(s) =>
        println(s.msgType)
        if (s.msgType == ApiResponseMsg.CreateUserWaiting){
          t.modState(s => s.copy(showNewAgentForm = false, showConfirmAccountCreation = true))
        } else {
          t.modState(s => s.copy(showNewAgentForm = false, showRegistrationFailed = true))
        }
      case Failure(s) =>
        println(s)
        t.modState(s => s.copy(showRegistrationFailed = true))
      // now you need to refresh the UI
    }
    t.modState(s => s.copy(showNewAgentForm = false))
  } else {
    t.modState(s => s.copy(showNewAgentForm = false))
  }
}

组件代码是:

val component = ReactComponentB[Props]("AddNewAgent")
.initialState(State()) // initial state from TodoStore
.backend(new Backend(_))
.renderPS(($, P, S) => {
  val B = $.backend
  <.div()(
    Button(Button.Props(B.addNewAgentForm(), CommonStyle.default, Seq(HeaderCSS.Style.SignUpBtn)),"Sign Up"),
     if (S.showNewAgentForm) NewAgentForm(NewAgentForm.Props(B.addNewAgent))        
    else   if (S.showConfirmAccountCreation  ) ConfirmAccountCreation(ConfirmAccountCreation.Props(B.confirmAccountCreation))

      else
      Seq.empty[ReactElement]
  )
})
//  .componentDidMount(scope => scope.backend.mounted(scope.props))
.configure(OnUnmount.install)
.build
def apply(props: Props) = component(props)
4

1 回答 1

3

对于任何寻找这个问题的答案的人来说,这里发生的事情是,modState未来完成中的调用永远不会被真正调用,因为它们返回一个不运行的回调。要解决此问题,您需要添加runNow()类似t.modState(s => s.copy(showNewAgentForm = false, showRegistrationFailed = true)).runNow()

于 2016-01-07T06:40:40.183 回答