Skip to content

Instantly share code, notes, and snippets.

@ih2502mk
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ih2502mk/a42fc432e7b42f772bd1 to your computer and use it in GitHub Desktop.
Save ih2502mk/a42fc432e7b42f772bd1 to your computer and use it in GitHub Desktop.
js-key-value-storage-objects
universal_getter = function(id, prop_name, cb) {
// get it from db or some data set
// if fails set erro to `new Error()`
//
// if storage is locked wait till it gets unlocked
//
// goGetItFromDB(prop_name, function(err, val) {
return cb(err, val);
// })
}
universal_setter = function(id, prop_name, val) {
// Lock storage
//
// goPutItIntoDB(prop_name, val, function(err, val) {
//
// Unlock storage
//
// })
}
var user = {};
Object.defineProperty(user, 'id', {
enumerable: true,
configurable: false, // No need to fiddle around with it
value: 123
})
Object.defineProperty(user, 'name', {
enumerable: true;
configurable: true;
get: function() {
var self = this;
return function(cb) {
universal_getter(self.id, 'name', cb);
}
},
set: function(val) {
universal_setter(this.id, 'name', val);
}
})
var name = user.name;
name(function(err, name) {
console.log(name);
})
// OR
// if you are into promises and stuff
//
// implying there was email defined the same way
var nodefn = require('when/node');
var name = nodefn.lift(user.name);
var email = nodefn.lift(user.email);
when.join(name, email).then(function(vals){
console.log('name is %s, and email is %s', vals[0], vals[1]);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment