Quick Way to work with Models and Alloy in Appcelerator Titanium
For those who have taken the dive into Alloy from Appcelerator, understanding how models work can definitely simplify your overall application architecture and then you can also leverage the functionality of Backbonejs Models.
Updated version of Blog Post can be found here Quick Way to work with Models and Alloy in Appcelerator Titanium Part Two
Easily Saving the Models
When I am working and want to get something going quickly with models, I hate to create the additional files to just get access to a model so I use this simple hack below.
Here is some background information on Backbonejs Sync functionality
Configuring the Models
Then you can just create your model or collection inline with the rest of your code and there is no need to create seperate model files. I use this approach when I am doing some quick hacking and need to work with models
Cleaning Up the Models
But since I am using this approach to quickly test out some code, you need to find a way to clean this data up after you create it. The trick here is to understand how the models are created by looking into the properties.js sync adapter and you will see that the property name is a combination of the collection_name property and the id created for the model.
With that knowledge, we extend the collection to have a function that will delete all of the models
Then to cleanup, all we need to do is create a collection and call the cleanup method