Welcome back to the Axiom Tutorial. In this installment, we'll flesh out our simple blog a bit and take you on a tour of more core Axiom features and concepts.
Editing
Right now, we've got a sort of write-only blog. Once written, there's no way to edit out those unfortunate comments you made about your boss in the middle of your otherwise pristine intellectual essay on early American cobbler recipes. Let's change that.
Updating an existing object in Axiom is as easy as the aforementioned pie. Once you've gotten ahold of an object from the database, you simply set the value of its properties like any other Javascript object. At the end of the request, any changes made to an object are automatically persisted for you. It's all transactional as well- if your code calls something in your ex-coworker's poorly written library that hacks up a NullPointerException hairball, then the changes made in that request are automatically rolled back.
With that in mind, let's make a quick function to change the values of an Entry. In some .js file under Entry:
function update(){
this.title = req.data.title;
this.body = new XMLList(req.data.body);
return 'Changes to entry saved. <a href="'+this.getURI()+'">View Entry</a>';
}
All we're doing here is assigning the values from the request data (what'll come from our form post) to properties on our Entry. All the items on req.data are Strings, so we create an E4X XML fragment for the body using the XMLList constructor before we assign it to the body property (which has a type of XHTML, remember?)
Now, we just need an edit form. Drop this in edit_entry.tal under Entry:
<html xmlns:tal="http://axiomstack.com/tale">
<head><title>Edit</title></head>
<body>
<form tal:attr="action: this.getURI('update')" method="post">
<label for="title">Title:</label>
<input name="title" tal:attr="value: this.title"/>
<label for="body">Body:</label>
<textarea name="body" tal:content="this.body"/>
<input type="submit" value="Save Changes"/>
</form>
</body>
</html>
Expose these methods in Entry's security.properties (again, see Security Model for a discussion of how to do proper login-based security):
update = @Anyone edit_entry = @Anyone
And finally, let's add a link to our edit form from the entry's display method. In Entry/entry_content.tal, add the follow after the last <p>:
<a tal:attr="href: this.getURI('edit_entry')">Edit This Entry</a>
Now, go back to the home page (http://localhost/simple-blog/home/). You'll see an "Edit This Entry" link on each entry, linking to the functionality we just created!
Deleting
We've covered creating and editing objects, but what about deleting them? In Axiom, an object is deleted by simply removing it from the object tree. At the end of a request, any object that isn't a child of some other object is deleted, along with all of its descendants.
Let's provide a way to delete Entries. Add the following method to a .js file under the Entry directory:
/**
* Delete this Entry.
*/
function delete_entry(){
this._parent.remove(this);
return this.title +" deleted.";
}
Add a link to this delete method in the entry content TALE. In Entry/entry_content.tal, right next to our Edit This Entry link, add the following:
<a tal:attr="href: this.getURI('delete_entry')">Delete This Entry</a>
And since Axiom is secure by default, we'll add open up our new delete method via Entry's security.properties:
delete_entry = @Anyone
When a user click the link on an Entry, the entry will be deleted and they will recieve the confirmation message returned in the delete_entry function.
That's All, Folks!
By now, you should be ready to start building your own web applications in Axiom. Where to go next? We recommend checking out the full-featured blog application packaged with the stack, browsing the rest of our documentation and enjoying a cold beverage of your choice.
