Skip to content

Instantly share code, notes, and snippets.

@tzmartin
Created October 22, 2014 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzmartin/5fc5beb37ca06b0a0717 to your computer and use it in GitHub Desktop.
Save tzmartin/5fc5beb37ca06b0a0717 to your computer and use it in GitHub Desktop.
Loop through an HTTP response object and only add new Alloy Models if needed.
/*
Loop through an HTTP response object and only add new Alloy Models if needed.
Assumes `eventId` is the primary key defined in Events.js Model file.
Caution: this snippet is untested
*/
var events = Alloy.createCollection('Events');
// Optional: Calling reset() without passing any models as arguments will empty the entire collection.
events.reset();
// Loop through HTTP results
_.each(response.results, function(element, index, list) {
Ti.API.info(element.eventId);
mdl = Alloy.createModel(element);
// If the model does not yet have an id, it is considered to be new. Could also leverage mdl.hasChanged()
if (mdl.isNew()) {
mdl.save({
success: function() {}
error: function(){}
});
}
});
// After persisting, hydrate the collection (invoking UI binding)
events.fetch();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment