Skip to content

Instantly share code, notes, and snippets.

@michieldewilde
Created November 12, 2015 15:05
Show Gist options
  • Save michieldewilde/51960565f76fcce3a40b to your computer and use it in GitHub Desktop.
Save michieldewilde/51960565f76fcce3a40b to your computer and use it in GitHub Desktop.
module.exports = function(Challenge) {
Challenge.on('dataSourceAttached', function(obj) {
var find = Challenge.find;
Challenge.find = function(filter, cb) {
return find.apply(this, arguments);
};
});
};
module.exports = function(app) {
var Challenge = app.models.Challenge;
var Recipe = app.models.Recipe;
var Restaurant = app.models.Restaurant;
var playerChallenge = app.models.playerChallenge;
var find = Challenge.find;
var cache = {};
Challenge.find = function(filter, cb) {
var key = '';
if (filter) {
key = JSON.stringify(filter);
}
var cachedResults = cache[key];
if (cachedResults) {
process.nextTick(function() {
cb(null, cachedResults);
});
} else {
var result = [];
Recipe.find().then(function(recipes) {
result = recipes;
return Restaurant.find();
}).then(function(restaurants) {
return result.concat(restaurants);
}).then(function(results) {
find.call(Challenge, function(err, r) {
if (!err) {
cache[key] = results;
}
console.log(cb);
cb(err, results);
});
});
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment