Skip to content

An intuitive simple class constructor with extendable features

License

Notifications You must be signed in to change notification settings

GSchutz/classy.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

  • String name: is the name of the class, can be access by MyClass.$name;
  • Object options: a set of initial properties for MyClass;
  • Object superclass: a set of personalized methods that can extend Classy or 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());
// → []

About

An intuitive simple class constructor with extendable features

Resources

License

Stars

Watchers

Forks

Packages

No packages published