在scalajs-react中设置ajax成功状态

时间:2016-01-05 09:27:31

标签: scala.js scalajs-react

我试图在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)

1 个答案:

答案 0 :(得分:2)

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