Created
November 12, 2015 15:05
-
-
Save michieldewilde/51960565f76fcce3a40b 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
module.exports = function(Challenge) { | |
Challenge.on('dataSourceAttached', function(obj) { | |
var find = Challenge.find; | |
Challenge.find = function(filter, cb) { | |
return find.apply(this, arguments); | |
}; | |
}); | |
}; |
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
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