Created
July 6, 2014 18:33
-
-
Save cramhead/dc431ab0417fce7aa965 to your computer and use it in GitHub Desktop.
Adding and removing collection-hooks in a meteor app
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
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