Skip to content

Instantly share code, notes, and snippets.

@frozeman
Forked from anonymous/gist:5767636
Last active December 18, 2015 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frozeman/5767649 to your computer and use it in GitHub Desktop.
Save frozeman/5767649 to your computer and use it in GitHub Desktop.
DB = {};
DB.movies = new Meteor.Collection('movies');
DB.ratings = new Meteor.Collection('ratings');
/*
* OfflineCollections, works as wrapper to easily clean your local collections
*/
OfflineCollections = (function(){
// PRIVATE
var _this = this;
_this.documentIds = {};
// wrapp the Collection.insert() method, to store IDs
var wrappedFind = Meteor.Collection.prototype.insert;
Meteor.Collection.prototype.insert = function () {
var documentId = wrappedFind.apply(this, arguments);
var collectionName = this._name;
if(!documentIds[collectionName])
documentIds[collectionName] = [];
// ADD the DOCUMENT ID to the OfflineCollections
documentIds[collectionName].push(documentId);
return documentId;
};
// PUBLIC
return {
clean: function(collectionName) {
// DELETE ALL DOCUMENTS from tLOCAL DB
_this.documentIds[collectionName] = _.reject(_this.documentIds[collectionName], function(documentId) {
DB[collectionName].remove({_id: documentId}, function(error) {
// TODO use Q to send if its successfull got removed
});
return true;
});
return _.isEmpty(_this.documentIds[collectionName]);
}
}
})();
// Use it like this
OfflineCollections.clean('ratings');
@frozeman
Copy link
Author

TODO use Q to send if its successfull got removed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment