Documentation

include MVC MVC.Animate MVC.Class MVC.Controller MVC.Controller.Action MVC.Controller.Action.Drag MVC.Controller.Action.Drop MVC.Controller.Action.EnterLeave MVC.Controller.Action.Event MVC.Controller.Action.Hover MVC.Controller.Action.Lasso MVC.Controller.Action.Selectable MVC.Controller.Action.Subscribe MVC.Controller.Comet MVC.Controller.Params MVC.Controller.Params.Drag MVC.Controller.Params.Drop MVC.Controller.Stateful MVC.Delegator MVC.Doc MVC.Element MVC.Event MVC.File MVC.History MVC.IO MVC.IO.Ajax MVC.IO.Comet MVC.IO.JsonP MVC.IO.WindowName MVC.IO.XDoc MVC.Model MVC.Model.Ajax MVC.Model.Cookie MVC.Model.JsonP MVC.Model.JsonRest MVC.Model.WindowName MVC.Model.XmlRest MVC.Native MVC.Native.Array MVC.Native.Date MVC.Native.Function MVC.Native.Number MVC.Native.Object MVC.Native.String MVC.Options MVC.Store MVC.SyntheticEvent MVC.Test MVC.Test.Assertions MVC.Test.Controller MVC.Test.Functional MVC.Test.Runner MVC.Test.Unit MVC.Timer MVC.Timer.Easings MVC.Vector MVC.View MVC.View.Helpers OpenAjax

MVC.Class

Class provides simple simulated inheritance in JavaScript. It is based off John Resig's Simple Class Inheritance library. Besides prototypal inheritance, it adds a few important features:
  • Static inheritance
  • Class initialization callbacks
  • Introspection

Examples

Basic example

Creates a class with a className (used for introspection), static, and prototype members:
 Monster = MVC.Class.extend('monster',
 /* @static *|
 {
   count: 0
 },
 /* @prototype *|
 {
   init : function(name){
     this.name = name;
     this.Class.count++
   }
 })
 hydra = new Monster('hydra')
 dragon = new Monster('dragon')
 hydra.name        // -> hydra
 Monster.count     // -> 2
 Monster.className // -> 'monster'
Notice that the prototype init function is called when a new instance of Monster is created.

Static property inheritance

Demonstrates inheriting a class poperty.
 First = MVC.Class.extend(
 {
     static_method : function(){ return 1;}
 },{})
 Second = First.extend({
     static_method : function(){ return this._super()+1;}
 },{})
 Second.static_method() // -> 2

Introspection

Often, it's nice to create classes whose name helps determine functionality. Ruby on Rails's ActiveRecord ORM class is a great example of this. Unfortunately, JavaScript doesn't have a way of determining an object's name, so the developer must provide a name. For example, Documentation's directives use their className to be added to different comment-code pairs in the appropriate way. Just by defining:
 MVC.Doc.Directive.Author = MVC.Class.extend('author');
you tell the documentation engine to look for @author and whatever comes after it as this.author. className is saved as a static property. You can access it from instance methods like:
 this.Class.className

Construtors

Class uses static and class initialization constructor functions.
 MyClass = MVC.Class.extend(
 {
   init: function(){} //static constructor
 },
 {
   init: function(){} //prototype constructor
 })
The static init constructor is called after a class has been created, but before extended is called on its base class. This is a good place to add introspection and similar class setup code. The prototype callback is called whenever a new instance of the class is created.

Constructor

MVC.Class

new MVC.Class() -> mvc.class

Creating a new instance of an object that has extended MVC.Class calls the init prototype function and returns a new instance of the class.

Static Methods

className

The name of the class provided for introspection purposes.

extend

extend(className, klass, proto) -> MVC.Class
Extends a class with new static and prototype functions. There are a variety of ways to use extend:
 //with className, static and prototype functions
 MVC.class.extend('task',{ STATIC },{ PROTOTYPE })
 //with just static and prototype functions
 MVC.class.extend({ STATIC },{ PROTOTYPE })
 //with just classname and prototype functions
 MVC.class.extend('task',{ PROTOTYPE })
 //with just prototype functions
 MVC.class.extend({ PROTOTYPE })
 //With just a className
 MVC.class.extend('task')
{String} - the classes name (used for classes w/ introspection)
{Object} - the new classes static/class functions
{Object} - the new classes prototype functions
{MVC.Class} - returns the new class

extended

extended(Class) -> undefined
Called with whatever classes extend your class
{MVC.Class} - the extending class.

init

init(class) -> undefined
Called when a new Class is created
{MVC.Class} - the new class

Prototype Methods

Class

Access to the static properties of the instance's class.

init

init() -> undefined
Called with the same arguments as new Class(arguments ...) when a new instance is created.