CRUD with RestfulX: Part 2/2 – Create, Update, and Delete

In my last article about CRUD with RestfulX I wrote about how to retrieve data from the Rails backend. In this article I’ll show you how the other CRUD operations (create, update, and delete) works with RestfulX.

I’ll keep using the same project as example and explore the same concepts too. It would be useful if you read my last post about how to retrieve data from the Rails backend with RestfulX.

Create

When we instantiate a model defined by model.yml (RestfulX), this instance exists only in the Flex side, and is not automatically created on the database. In order to save this instance to the database, you need to call the create method (again, sorry for examples in portuguese).

// Instantiates a new Participante and define its properties
var p1:Participante = new Participante();
p1.nome = "Antônio da Silva";
p1.email = "antonio@exemplo.com";
p1.data_registro = new Date;

// Creates this object into the database
p1.create( onCreateSuccess, onCreateFailure );

At the line 2 we instantiate a new Participante. In the lines 3, 4, and 5 we define fictitious values to the object that will be saved to database. Finally, at line 8, we call the create method, responsible for to send a REST request to the server to create this object into the database.

As well as the second parameter the index method receives, the first one on create method also accepts an Array of options or the function that should be called when the create operation completes successfully. The second parameter is the function called if some error occur during the create operation. Any of these parameters are mandatory.

The function called when the operation completes successfully receives only one parameter, that has the same type the object being created does. The function called when some error occur also receives only one parameter, a FaultEvent instance. So the callback functions could be defined like this:

private function onCreateSuccess( participante:Participante ):void
{
	// Code executed when the object is created successfully on database
}

private function onCreateFailure( event:FaultEvent ):void
{
	// Code executed when some error occur during the create operation
}

These few lines of code are enough to create a record into the database – remember that the callback functions are not mandatory.

Update

To update a record with RestfulX is as simple as to create them: you only need to change the object’s properties and call the method update, using the same parameters we used in the create example.

Look how easy it is!

// Selects the Participante with id = 1 (lets assume it exists)
var p1:Participante = Rx.cached(Participante).withId(1);
// Change its e-mail
p1.email = "newemail@exemplo.com";
// Save the changes into the database
p1.update( onUpdateSuccess, onUpdateFailure );

// The callback functions
private function onUpdateSuccess( participante:Participante ):void
{
	// Code executed when the record is saved successfully
}

private function onUpdateFailure( event:FaultEvent ):void
{
	// Code executed if an error occur during the update function
}

Delete

How about to delete a record? Easy! Use the object’s destroy method, passing again the two optional callback functions as parameters to this method.

Lets destroy the record we used in last example:

// Gets the Participante with id = 1
var p1:Participante = Rx.cached(Participante).withId(1);
// Deletes its record on the database
p1.destroy( onDestroySuccess, onDestroyFailure );

// Callback functions
private function onDestroySuccess( participante:Participante ):void
{
	// Code executed when the record is successfully deleted from database
	Alert.show("The participant " + participante.nome + " has been removed from database successfully.");
}

private function onDestroyFailure( event:FaultEvent ):void
{
	// Code executed when some error occur during the destroy operation
}

The onDestroySuccess function receives one parameter of the same type the object being destroyed. Weird? Maybe. The record is just destroyed, but you receive its instance so you can use it to say you “last goodbye”, as in the 10th line in the last example, using the Participante’s name to compose a message to the user.

Conclusion

With these examples we could see that CRUD operations are taken easy with RestfulX. Once the CRUD operations are about 90% of any system development (in my opinion) you’ll have many extra time to care about the system’s intelligence and usability. I showed you simple CRUD operations, but RestfulX have many other features to work even with non-REST server requests.

For more info, please see the RestfulX’s documentation. The API documentation page that contains the methods used in these examples is here.

Soon I’ll post this example as a working Flex application. Follow me and stay tunned: @elvisfernandes!


Poderia avaliar este artigo, por favor?

1 Estrela2 Estrelas3 Estrelas4 Estrelas5 Estrelas (Nenhum voto)
Loading ... Loading ...

Posts relacionados