Quick Way to work with Models and Alloy in Appcelerator Titanium Pt II - Clearly Innovative | Web and Mobile DevelopmentClearly Innovative | Web and Mobile Development

This is a blog post about a question I saw on the Q&A forums

 I have a simple app where I want to have a collection of countries for each continent, but whenever I create a model / save it, and add it to a collection, it appears in all of them. Both collections seem to get all the models of type “country”. I am using the properties adapter to persist.

And a old blog post I had written about working with models without the models and collection file. http://www.clearlyinnovative.com/blog/post/34758524608/quick-way-to-work-with-models-and-alloy-in-appcelerator-titanium

The magic here is to allow the model/collection pair to control the information that is set in the config block for each instance of the object. When working with Alloy model files, you must specify the configuration block in the source file

The code below uses a function to create the model/collection object and set the configuration block when the new object is created

Now the model knows what collection name it belong to, so when we are creating a collection, we tell it what model it should be associated with

Here is the complete code listing

when you run the code you should get output in your log like this.

You will notice that each collection only has one item in it and they are properly stored in the right collection What we have done is create a model factory that will allow you to specify the correct