Created
June 12, 2013 18:03
-
-
Save anonymous/5767636 to your computer and use it in GitHub Desktop.
OfflineCollections, works as wrapper to easily clean your local collections
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) { | |
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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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