Skip to content

Instantly share code, notes, and snippets.

@brianleroux
Created October 13, 2010 22:53
Show Gist options
  • Save brianleroux/625120 to your computer and use it in GitHub Desktop.
Save brianleroux/625120 to your computer and use it in GitHub Desktop.
function Person() {
// properties and validations
attr(
{ id:Number, unique:true, nullable:false },
{ email:String, unique:true, nullable:false, min:1, max:55, format:'[a-b]' },
{ salt:String },
{ pswd:String },
{ active:Boolean, init:false },
{ tags:Array }
);
// helpful property declarations
timestamp();
// callbacks
creating({ before:poundSalt, after:emailActivationCode });
updating();
deleting();
// a private function to generate a unique salt for hashing the password, called in 'creating' callback
// testable therefore by the creating callback?
var poundSalt = function(obj) {
};
// emails an activation code
var emailActivationCode = function(obj) {
};
var forgotPassword = function(obj) {
};
// public instance attributes
this.prototype = {
// activates the user account
get active() {
},
set active() {
}
}
// views, beautiful custom finders
view('tags', { map:function(){}, reduce:function(){} });
view('popular');
};
@brianleroux
Copy link
Author

ha! so it is. no, no magic. again this is just playing around. proto is probably the desired behavior to do but... whatever. Did I mention that this is toy code? =) Anyhow, I do appreciate the thorough debug. Any thoughts on the API style?

@creationix
Copy link

Brian, so are you using with internally to make all the magic attr timestamp and friends. If so, could we get close to this syntax but still be es5 strict capable?

Also if you are doing a demcompile + with + eval trick to change the scope (like here http://gist.github.com/199372), then this will cause issues with people using the library, because suddenly they can't get to closures from their local file.

Maybe instead of this.prototype = {…} just return the object you want exposed. Then the model system will call the function with a custom "this" scope and that's where you can put the magic meta functions. Also have a chance to modify the object after it's created and returned.

@brianleroux
Copy link
Author

I left work so I actually do not remember how I made it sort work but essentially it was some runtime recompilation. Yes: evil. But this is just a thought experiment.

Another way would be to create a magical methods() method that accepts an obj for mixing into the __proto__. Meh.

The combinations of Proxy and Reflect make this sort of business pretty easy... I do hope those innovations make it into the language proper.

@creationix
Copy link

I created a fork with some style changes. Also I noticed the views section. What exactly do they do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment