Skip to content

Instantly share code, notes, and snippets.

@aaronksaunders
Last active December 18, 2015 11:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronksaunders/75a3e23cda8a830d31fc to your computer and use it in GitHub Desktop.
Save aaronksaunders/75a3e23cda8a830d31fc to your computer and use it in GitHub Desktop.
Quick Way to work with Models and Alloy in Appcelerator Titanium
// set the sync adapter
Alloy.Backbone.sync = function(method, model, opts) {
require("alloy/sync/properties").sync(method, model, opts);
};
// createing a model now just requires the collection_name field
var User = Model.extend({
config : {
adapter : {
collection_name : 'users'
}
}
});
// extend ALL Collections and add the cleanup function
var Collection = Alloy.Backbone.Collection.extend({
/**
* clean up any models from the properties db
*/
cleanup : function() {
var regex = new RegExp("^(" + this.config.adapter.collection_name + ")\\-(.+)$");
var TAP = Ti.App.Properties;
_.each(TAP.listProperties(), function(prop) {
var match = prop.match(regex);
if (match) {
TAP.removeProperty(prop);
Ti.API.info('deleting old model ' + prop);
}
});
}
});
// creating a collection now just requires the collection_name field
var UserCollection = Collection.extend({
config : {
adapter : {
collection_name : 'users'
}
}
});
// cleanup all of the old data associated with the collection
new UserCollection().cleanup();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment