Skip to content

Instantly share code, notes, and snippets.

@thebigredgeek
Created October 7, 2013 18:04
Show Gist options
  • Save thebigredgeek/6872315 to your computer and use it in GitHub Desktop.
Save thebigredgeek/6872315 to your computer and use it in GitHub Desktop.
var $q = {defer:function(){return {reject:function(){},resolve:function(){},promise:{}}}}
var db = {query:function(x,y,z){z();}};
var $rootScope = {$apply:function(fn){fn();}};
var x = {
getEvents: function() {
var matchCallback = null;
var reduce = null;
var deferred = $q.defer();
if (arguments.length === 1) {
matchCallback = arguments[0];
} else if (arguments.length === 2) {
matchCallback = arguments[0];
reduce = arguments[1];
}
if (matchCallback) {
console.log('got a match callback: ', matchCallback);
} else {
console.log('no match callback.');
}
if (reduce) {
console.log('got a reduce function: ', reduce);
} else {
console.log('no reduce function.');
}
var map = function(doc) {
if (matchCallback != null) {
console.log('matchCallback = ', matchCallback);
if (!matchCallback(doc)) {
return;
}
}
if (doc.type === 'event') {
if (options && options.useKey) {
emit(doc[options.useKey], doc);
} else {
emit(doc.createdBy, doc);
}
} else {
console.log('Not an event:', doc);
}
}
var options = {};
if (reduce) {
options.reduce = reduce;
} else {
options.reduce = true;
}
db.query({map: map}, options, function(err, res) {
$rootScope.$apply(function() {
if (err) {
log.error(err);
deferred.reject(err);
} else {
deferred.resolve(res);
}
});
});
return deferred.promise;
}};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment