Created
October 22, 2014 18:50
-
-
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.
This file contains hidden or 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
/* | |
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