-
-
Save justsml/aaddc9852c1624d61cf3 to your computer and use it in GitHub Desktop.
A classy ES6 example: "Do it NOW!" - Shia
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
// 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