简体   繁体   English

通过JavaScript重定向时如何保持StatefulSnippet var值

[英]How to keep StatefulSnippet var values when redirecting through Javascript

I have an image link that when i click it i want to do some changes in some class variables and then redirect to other page and use those values. 我有一个图像链接,当我单击它时,我想对某些类变量进行一些更改,然后重定向到其他页面并使用这些值。

Here is my code, im using StatefulSnippet 这是我的代码,即时通讯使用StatefulSnippet

class MySnippet extends StatefulSnippet {

  var selectedStudent: Student=null

  def students =
    ".tabla" #> studentsList.map { s =>
      "#id" #> s.Id &
      "#name" #> s.Name & 
      "#surname" #> s.Surname &
      "#buttonAcademic [src]" #> tickUrl &
      ".clickableAcademic [onclick]" #> SHtml.onEvent(onClickCallbackAcademic(s))
    }

  def onClickCallbackAcademic(student: Student,xhtml:NodeSeq)(s: String): JsCmd = {
    //makes some stuff to student
    selectedStudent = student
    JsCmds.RedirectTo("/Academic")
  }

}

But when I redirect to the other page, my var selectedStudent is null, i tried debugging and selectedStudent has the correct value but when redirecting it turns null. 但是,当我重定向到另一个页面时,我的var selectedStudent为null,我尝试调试,并且selectedStudent具有正确的值,但是在重定向时它将变为null。 Any ideas? 有任何想法吗?

StatefulSnippet using HTML form and hidden parameter to pass information (which snippet instance should be used) to server side of Lift. StatefulSnippet使用HTML表单和隐藏参数将信息(应使用哪个片段实例)传递到Lift的服务器端。

So a JS redirect will not work, because it will not pass those information. 因此,JS重定向将不起作用,因为它不会传递这些信息。

Here are some solution comes in my mind that may work in this case: 我想到了一些在这种情况下可能有效的解决方案:

  • Wrapping a form outside of and submit it by JS in your onClickCallbackAcademic. 在您的onClickCallbackAcademic中包装表单并由JS提交。

  • Instead of using StatefulSnippet, use SessionVar to store your selectedStudent, so you could use it without form submit. 代替使用StatefulSnippet,使用SessionVar存储您的selectedStudent,这样您就可以在不提交表单的情况下使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM