Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Last active March 15, 2017 18:50
Show Gist options
  • Save aaronksaunders/edf9d1a3c637cef42136 to your computer and use it in GitHub Desktop.
Save aaronksaunders/edf9d1a3c637cef42136 to your computer and use it in GitHub Desktop.
Alternate Implementation Appcelerator Titanium Models/Collections

Implementing Appcelerator Titanium Models/Collections for Cloud Services without all the extra files

It is a PITA working with Appecelerator Alloy Models and Collection because you need to create these template files for everything, even of you are not using them. This is a "HACK" that I came up with after a few hours that appears to address the issue.

I am certain that there might be a cleaner way, which would require deeper understand of the underpinnings of Alloy, maybe someone can make a suggestion, maybe it will come to me later.

###Notes This code is using a ACS Sync Adapter customize for cloud services with promises integration

https://github.com/aaronksaunders/Appcelerator-Cloud-Services-Sync-Adapter

//
// helper function to create model/collection objects without all the files
//
function AlloyObjectFactory(_name, _Object) {
var model,
definition = {
config : {
adapter : {
type : "acs"
},
settings : {
object_name : _name /* "clothing" - name to be associated with the object */,
object_method : _Object /*"Objects" - this is a Custom ACS Object */
}
},
extendModel : function(Model) {
_.extend(Model.prototype, {});
return Model;
},
extendCollection : function(Collection) {
_.extend(Collection.prototype, {});
return Collection;
}
};
model = Alloy.M(_name, definition, []);
return {
Model : model,
Collection : Alloy.C(_name, definition, model)
};
}
// create the Model and Collection for Clothing Object.
// Need to find a cleaner way to do this, my Javascript mojo is
// weak since it has been a long day!
var ClothingCollection = new AlloyObjectFactory('clothing', "Objects").Collection;
var ClothingModel = new AlloyObjectFactory('clothing', "Objects").Model;
// we can use Appcelerator Cloud Services object also...
// var PhotoCollection = new AlloyObjectFactory('photo', "Photos").Collection;
// var PhotoModel = new AlloyObjectFactory('photo', "Photos").Model;
// create a new model object
var clothing = new ClothingModel();
// set parameters for data for object
var clothesData = {
type : 'suit',
color : 'grey pinstripe',
tags : ['work', 'wool', 'grey']
};
// Save object - ACS adapter has promises implemented
clothing.save(clothesData).then( function( _clothesModel ){
console.info('addClothes - ' + JSON.stringify(_clothesModel));
}, function(_error){
console.error(JSON.stringify(_error));
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment