我正在尝试使用Julia的Blink(1.02)创建一个简单的登录页面。
我有一个有效的login_button和一个处理按钮的处理函数(我认为),但是我无法通过“ handle(w,“ press”)do args ...“内部的函数来做任何有用的事情。
我希望处理程序函数在检查用户名和密码后运行“ body!(w,new_page)”,但是它只是在打印出“按下login_button!”后挂起。之后按钮停止响应。
我已经为此苦苦挣扎了一段时间,感谢您的帮助。
旧朱莉娅代码:
using WebIO, Blink
page =
node(:div,
node(:p, "please login below.", attributes=Dict("class"=>"lead")),
node(:input, attributes=Dict("id"=>"username", "type"=>"text", "placeholder"=>"username")),
node(:input, attributes=Dict("id"=>"password", "type"=>"password", "placeholder"=>"password")),
node(:button, "LOGIN", attributes=Dict("id"=>"login_button")));
new_page = node(:div, "New page!");
w = Window()
body!(w, page)
@js_ w document.getElementById("login_button").onclick = () -> Blink.msg("press", "login")
handle(w, "press") do args...
println("login_button pressed!")
username = @js w document.querySelector("""input[id="username"]""").value
password = @js w document.querySelector("""input[id="password"]""").value
println("$username, $password")
if username == "user" && password == "pass"
body!(w, new_page)
else
@js alert("Incorrect username or password. Try again.")
end
end
新代码:
using WebIO, Blink, Interact
using ..User
function page_inputs()
username = textbox("enter username", attributes=Dict("size"=>50))
password = textbox("enter password", typ="password")
login_button = button("LOGIN")
Widget(["username"=>username,"password"=>password,"login_button"=>login_button])
end
inputs = page_inputs();
page =
node(:div,
node(:br),
node(:img, attributes=Dict("src"=>"https://elmordyn.files.wordpress.com/2012/07/20110223084209-beholder.gif")),
node(:h2, "beholder", attributes=Dict("class"=>"display-3")),
node(:p, "VERSION 0.2"),
node(:hr, attributes=Dict("class"=>"my-3")),
node(:p, "No unauthorized access. Please login below.", attributes=Dict("class"=>"lead")),
node(:div, inputs),
attributes=Dict(:align=>"middle"));
title = "LOGIN ~ beholdia"
size = (500, 600)
function validate_user(w, inputs)
if inputs["login_button"][] > 0
inputs["login_button"][] = 0
if inputs["username"][] in keys(User.users) && inputs["password"][] == User.users[inputs["username"][]]
println("""Logging $(inputs["username"][]) in...""")
return true
else
@js w alert("Incorrect username or password. Try again.")
return false
end
end
end
function events(w, inputs)
@async while true
validate_user(w, inputs) == true ? break : sleep(0.1)
end
end