Note that there are some explanatory texts on larger screens.

plurals
  1. POOpa: How to Interact With the Database Without Blocking the UI
    text
    copied!<h2>The Setup</h2> <p>I have a (rather standard) form that allows a user to create an account by entering a username and password. When the <code>#username</code> field loses focus, the database is queried to see if the supplied username is available. </p> <h2>The Problem</h2> <p>Everything works as expected, but the user interface is blocked from when the <code>onblur</code> event of <code>#username</code> is triggered until the database returns a value. How can check the database in an asyncronous and non-blocking fashion?</p> <p>Here's my code (I've pulled most of this code out of example Opa projects):</p> <h3>Types</h3> <pre><code>@abstract type User.ref = string type User.t = { username : string ; passwd : string } type User.map('a) = ordered_map(User.ref, 'a, String.order) </code></pre> <h3>Database</h3> <pre><code>db /user : User.map(User.t) </code></pre> <h3>Form</h3> <pre><code>&lt;form&gt; &lt;input id=#username type="text" onblur={_ -&gt; username_available() } placeholder="username" /&gt; &lt;input id=#password type="password" placeholder="password" /&gt; &lt;/form&gt; </code></pre> <h3>Actions</h3> <pre><code>username_available() : void = user = Dom.get_value(#username) match (get_user(user)) with | { none } -&gt; Log.notice("username available:", "YES") | { some = _ } -&gt; Log.notice("username available:", "NO") get_user(username : string) : option(User.t) = match ?/user[username] with | { none } -&gt; Option.none | { ~some } -&gt; Option.some(some) </code></pre> <p>Thanks for your help.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload