Skip to content

Instantly share code, notes, and snippets.

@cramhead
Created July 6, 2014 18:33
Show Gist options
  • Save cramhead/dc431ab0417fce7aa965 to your computer and use it in GitHub Desktop.
Save cramhead/dc431ab0417fce7aa965 to your computer and use it in GitHub Desktop.
Adding and removing collection-hooks in a meteor app
var hookHandles = [];
var initCollectionHooks = function() {
//once the seed process is complete or no seeding takes place add the listener
hookHandles.push(
Collectibles.after.insert(function(userId, doc) {
// get the imageIds and set them as being linked
App.emitter.emit('collectible.added', userId, doc);
})
);
hookHandles.push(
Collectibles.after.update(function(userId, doc, fieldNames, modifier, options) {
if (fieldNames.indexOf("favs") > -1) {
if (modifier.$push) {
collectibleFaved(userId, doc);
}
// no notification on $pop
}
})
);
};
var disableCollectionHooks = function() {
for (var i = hookHandles.length - 1; i >= 0; i--) {
var hookHandle = hookHandles.pop();
hookHandle.remove();
};
};
// start with hooks enabled
initCollectionHooks();
App.emitter.on('seeding.started', disableCollectionHooks);
App.emitter.on('seeding.completed', initCollectionHooks);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment