-
-
Save aaronksaunders/75a3e23cda8a830d31fc to your computer and use it in GitHub Desktop.
Quick Way to work with Models and Alloy in Appcelerator Titanium
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// set the sync adapter | |
Alloy.Backbone.sync = function(method, model, opts) { | |
require("alloy/sync/properties").sync(method, model, opts); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// createing a model now just requires the collection_name field | |
var User = Model.extend({ | |
config : { | |
adapter : { | |
collection_name : 'users' | |
} | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
}); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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