classy.js
v0.1.2
Published
A general purpose class constructor for javascript
Downloads
9
Readme
classy.js
An intuitive simple class constructor with extendable features.
Dependencies
Just Lodash ^3.10.0.
Usage
var User = Classy('User', {
$defaults: {
name: ''
}
});
var john = User({name: 'John'});
john.$add();
// or User.$add(john);
User.$data();
// → [{name: 'John'}]
john.$change('name', 'John Nash');
// → {name: 'John Nash'}
john.$remove();
User.$data();
// → []Documentation
Classy is the only variable exported.
Classy(name, [options, superclass])
Creates a Classy constructor that is composed from a ClassyBuilder.
Stringname: is the name of the class, can be access byMyClass.$name;Objectoptions: a set of initial properties forMyClass;Objectsuperclass: a set of personalized methods that can extendClassyor powerfull rewrite original methods;
Example
function SuperClass() {
var private = "This is my SuperClass";
this.myOwnMethod = function() {
// do something
return this;
};
this.$add = function(data) {
// set a property as required
if (data.email) {
// ok, continue, run the original method
// the arguments will be already available
return this.$super();
} else {
throw new Error('The property email is required');
}
};
}
var options = {
$pk: 'id',
$defaults: {
name: "My Default Name"
},
$hasMany: {}
}
var User = Classy( 'User', options, new SuperClass() );
var john = User({});
try {
john = john.$add();
} catch (e) {
console.log(e.message);
// → "The property email is required"
}
console.log(john);
// → {name: "My Default Name", id: 1}
console.log(User.$data());
// → []
