Last active
August 29, 2015 14:00
-
-
Save keithio/11152896 to your computer and use it in GitHub Desktop.
Parse.Object.previous() workaround for Parse.Cloud.beforeSave()
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
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