Skip to content

Instantly share code, notes, and snippets.

@justsml
Last active September 27, 2015 00:44
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 justsml/aaddc9852c1624d61cf3 to your computer and use it in GitHub Desktop.
Save justsml/aaddc9852c1624d61cf3 to your computer and use it in GitHub Desktop.
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