Skip to content

Instantly share code, notes, and snippets.

Created June 12, 2013 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/5767636 to your computer and use it in GitHub Desktop.
Save anonymous/5767636 to your computer and use it in GitHub Desktop.
OfflineCollections, works as wrapper to easily clean your local collections
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) {
console.log(DB[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

I made, just i case somebody wants to know this :), forgot to log in.

There is still a small detail:
See: // 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