-
-
Save frozeman/5767649 to your computer and use it in GitHub Desktop.
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
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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TODO use Q to send if its successfull got removed