Keeping Private Data Safe
The app features two views: a simple login/registration page and the actual list itself. The todo items each have an assigned priority level (an integer between 1 and 3) as well as an optional due date. To keep things simple, this is expressed as an integer in hours from when the task is created.
In this view we can see a few items of varying priority. It looks like we’ve already paid Derek back for sushi, so that’s good, but this post is still unfinished with a looming deadline, so let’s get at it.
Once the SDK is initialized, we can just start making API calls. The new JS SDK supports Restler-style chaining, so we get to write these with clean, semantic syntax. Here’s a login request, which is triggered when the user clicks the Login button in the UI.
We can easily show error messages from the server. Here, a bad email will cause show_error to flash this response:
We’re also using these same fields to offer registration. Just for the sake of the demo, we’re only asking for an email and a password again. Here’s how we might register a new user with CloudMine.
Notice how we are taking advantage of callback chaining. All you really need is the createUser call, but the extra stuff makes for a more user-friendly interface. We can listen for specific successes and errors and react to them differently. Here we chose to return the server’s raw error response when the email is already in use, but you can return our own message if the email is invalid. Since we also changed the UI to indicate the request is pending, we can reset it in the event of any error at all.
Storing and retrieving todos
Once we’re logged in, we can query for that user’s data to see if they already have a todo list.
Sending new and updated todos is just as easy. Here we’re sending a new item that was just created, stored in the object_data object.
When the user checks off an item, we can use the update method to change one of its properties.
We’ve also added little buttons. Here’s how simple it is to remove an object both in frontend and in backend by calling one function:
To see more examples, including saving user sessions and logging out, you can fork the finished app from its repo.