A classy ES6 example: "Do it NOW!" - Shia
// services/user.js | |
function Users(data) { | |
// ensure we're a real thing | |
if (!(this instanceof Users)) { return new Users(data); } | |
this.users = data || []; | |
} | |
Users.prototype.add = function(opts) { | |
// Validate input, We need to extract the 3 fields from opts | |
if ( !opts || typeof(opts) !== 'object' ) { | |
return Promise.reject('add() requires Opts parameter'); | |
} | |
// Unpack data, assuming keys are there | |
var name = opts.name, | |
email = opts.email, | |
pass = opts.password; | |
var hash = getSha256(pass); | |
return http.post('/users', { | |
'name': name, | |
'email': email, | |
'passwordHash': hash}) | |
.then(function(usr) { | |
return this.users.push(usr); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment