Skip to content

Instantly share code, notes, and snippets.

@keithio
Last active August 29, 2015 14:00
Show Gist options
  • Save keithio/11152896 to your computer and use it in GitHub Desktop.
Save keithio/11152896 to your computer and use it in GitHub Desktop.
Parse.Object.previous() workaround for Parse.Cloud.beforeSave()
Parse.Cloud.beforeSave('Post', function (request, respond) {
var post = request.object;
var prevCatId = post.get('prevCat').id;
var catId = post.get('cat').id;
if (post.dirty('cat')) {
var prev = new Parse.Query('Category');
prev.get(prevCatId, {
success: function (category) {
// Decrement old category post count.
category.increment('postCount', -1);
category.save();
// Update previous category manually in
// a separate field. Boo!
var prevCat = new Category();
prevCat.id = prevCatId;
post.set('prevCat', prevCat);
// Increment new category post count.
var next = new Parse.Query('Category');
next.get(catId, {
success: function (category) {
category.increment('postCount');
category.save();
response.success();
}
});
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment