Last active
August 29, 2015 14:14
-
-
Save ih2502mk/a42fc432e7b42f772bd1 to your computer and use it in GitHub Desktop.
js-key-value-storage-objects
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
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