Created
February 5, 2013 16:04
-
-
Save johannesboyne/4715413 to your computer and use it in GitHub Desktop.
Dynamic field/attribute hiding from an object. (I named it fieldHider because we used it to hide mongodb results)
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
// this is used to work in Node.js | |
// if you want to use it inside the browser, replace the map function to ensure older browser support. | |
// DEPENDENCIES: underscore! | |
function omitFields (model, hidden_fields) { | |
var hider_code = 'return _.omit(model'; | |
hidden_fields.map(function (hider) { | |
hider_code += ', \''+hider+'\''; | |
}); | |
hider_code += ');'; | |
return new Function('_', 'model', hider_code)(_, model); | |
} | |
// use | |
omitFields({name: "Mr. Johnson", password: "PWHASH", secret: "mystery"}, ['password', 'secret']); | |
// => return: {name: "Mr. Johnson"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment