diff --git a/class/class.js b/class/class.js index 18854c74..91acdfa5 100644 --- a/class/class.js +++ b/class/class.js @@ -1,4 +1,4 @@ -//jQuery.Class +//jQuery.Class // This is a modified version of John Resig's class // http://ejohn.org/blog/simple-javascript-inheritance/ // It provides class level inheritance and callbacks. @@ -17,12 +17,12 @@ steal("jquery","jquery/lang/string",function( $ ) { concatArgs = function(arr, args){ return arr.concat(makeArray(args)); }, - + // tests if we can get super in .toString() fnTest = /xyz/.test(function() { xyz; }) ? /\b_super\b/ : /.*/, - + // overwrites an object with methods, sets up _super // newProps - new properties // oldProps - where the old properties might be @@ -31,8 +31,8 @@ steal("jquery","jquery/lang/string",function( $ ) { addTo = addTo || newProps for ( var name in newProps ) { // Check if we're overwriting an existing function - addTo[name] = isFunction(newProps[name]) && - isFunction(oldProps[name]) && + addTo[name] = isFunction(newProps[name]) && + isFunction(oldProps[name]) && fnTest.test(newProps[name]) ? (function( name, fn ) { return function() { var tmp = this._super, @@ -60,41 +60,41 @@ steal("jquery","jquery/lang/string",function( $ ) { * @download dist/jquery/jquery.class.js * @test jquery/class/qunit.html * @description Easy inheritance in JavaScript. - * + * * Class provides simulated inheritance in JavaScript. Use clss to bridge the gap between - * jQuery's functional programming style and Object Oriented Programming. It + * jQuery's functional programming style and Object Oriented Programming. It * is based off John Resig's [http://ejohn.org/blog/simple-javascript-inheritance/|Simple Class] * Inheritance library. Besides prototypal inheritance, it includes a few important features: - * + * * - Static inheritance * - Introspection * - Namespaces * - Setup and initialization methods * - Easy callback function creation - * - * + * + * * The [mvc.class Get Started with jQueryMX] has a good walkthrough of $.Class. - * + * * ## Static v. Prototype - * + * * Before learning about Class, it's important to * understand the difference between * a class's __static__ and __prototype__ properties. - * + * * //STATIC * MyClass.staticProperty //shared property - * + * * //PROTOTYPE * myclass = new MyClass() * myclass.prototypeMethod() //instance method - * + * * A static (or class) property is on the Class constructor * function itself - * and can be thought of being shared by all instances of the + * and can be thought of being shared by all instances of the * Class. Prototype propertes are available only on instances of the Class. - * + * * ## A Basic Class - * + * * The following creates a Monster class with a * name (for introspection), static, and prototype members. * Every time a monster instance is created, the static @@ -141,18 +141,18 @@ steal("jquery","jquery/lang/string",function( $ ) { * * @codeend * - * + * * Notice that the prototype init function is called when a new instance of Monster is created. - * - * + * + * * ## Inheritance - * + * * When a class is extended, all static and prototype properties are available on the new class. * If you overwrite a function, you can call the base class's function by calling * this._super. Lets create a SeaMonster class. SeaMonsters are less * efficient at eating small children, but more powerful fighters. - * - * + * + * * Monster("SeaMonster",{ * eat: function( smallChildren ) { * this._super(smallChildren / 2); @@ -161,15 +161,15 @@ steal("jquery","jquery/lang/string",function( $ ) { * this.health -= 1; * } * }); - * + * * lockNess = new SeaMonster('Lock Ness'); * lockNess.eat(4); //health = 12 * lockNess.fight(); //health = 11 - * + * * ### Static property inheritance - * + * * You can also inherit static properties in the same way: - * + * * $.Class("First", * { * staticMethod: function() { return 1;} @@ -180,36 +180,36 @@ steal("jquery","jquery/lang/string",function( $ ) { * },{}) * * Second.staticMethod() // -> 2 - * + * * ## Namespaces - * + * * Namespaces are a good idea! We encourage you to namespace all of your code. * It makes it possible to drop your code into another app without problems. * Making a namespaced class is easy: - * - * + * + * * $.Class("MyNamespace.MyClass",{},{}); * * new MyNamespace.MyClass() - * - * + * + * *

Introspection

- * + * * Often, it's nice to create classes whose name helps determine functionality. Ruby on * Rails's [http://api.rubyonrails.org/classes/ActiveRecord/Base.html|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. Class fixes this by taking a String name for the class. - * + * * $.Class("MyOrg.MyClass",{},{}) * MyOrg.MyClass.shortName //-> 'MyClass' * MyOrg.MyClass.fullName //-> 'MyOrg.MyClass' - * + * * The fullName (with namespaces) and the shortName (without namespaces) are added to the Class's * static properties. * * * ## Setup and initialization methods - * + * *

* Class provides static and prototype initialization functions. * These come in two flavors - setup and init. @@ -233,23 +233,23 @@ steal("jquery","jquery/lang/string",function( $ ) { * @codeend * * ### Setup - * + * * Setup functions are called before init functions. Static setup functions are passed * the base class followed by arguments passed to the extend function. - * Prototype static functions are passed the Class constructor + * Prototype static functions are passed the Class constructor * function arguments. - * + * * If a setup function returns an array, that array will be used as the arguments * for the following init method. This provides setup functions the ability to normalize * arguments passed to the init constructors. They are also excellent places * to put setup code you want to almost always run. - * - * + * + * * The following is similar to how [jQuery.Controller.prototype.setup] * makes sure init is always called with a jQuery element and merged options * even if it is passed a raw * HTMLElement and no second parameter. - * + * * $.Class("jQuery.Controller",{ * ... * },{ @@ -261,16 +261,16 @@ steal("jquery","jquery/lang/string",function( $ ) { * options || {} ) ] * } * }) - * + * * Typically, you won't need to make or overwrite setup functions. - * + * * ### Init * * Init functions are called after setup functions. * Typically, they receive the same arguments * as their preceding setup function. The Foo class's init method * gets called in the following example: - * + * * $.Class("Foo", { * init: function( arg1, arg2, arg3 ) { * this.sum = arg1+arg2+arg3; @@ -278,22 +278,22 @@ steal("jquery","jquery/lang/string",function( $ ) { * }) * var foo = new Foo(1,2,3); * foo.sum //-> 6 - * + * * ## Proxies - * + * * Similar to jQuery's proxy method, Class provides a * [jQuery.Class.static.proxy proxy] * function that returns a callback to a method that will always * have * this set to the class or instance of the class. - * - * + * + * * The following example uses this.proxy to make sure * this.name is available in show. - * + * * $.Class("Todo",{ - * init: function( name ) { - * this.name = name + * init: function( name ) { + * this.name = name * }, * get: function() { * $.get("/stuff",this.proxy('show')) @@ -303,24 +303,24 @@ steal("jquery","jquery/lang/string",function( $ ) { * } * }) * new Todo("Trash").get() - * + * * Callback is available as a static and prototype method. - * + * * ## Demo - * + * * @demo jquery/class/class.html - * - * + * + * * @constructor - * + * * To create a Class call: - * + * * $.Class( [NAME , STATIC,] PROTOTYPE ) -> Class - * + * *

*
{optional:String} - *

If provided, this sets the shortName and fullName of the - * class and adds it and any necessary namespaces to the + *

If provided, this sets the shortName and fullName of the + * class and adds it and any necessary namespaces to the * window object.

*
*
{optional:Object} @@ -331,25 +331,25 @@ steal("jquery","jquery/lang/string",function( $ ) { *

Creates prototype methods on the class.

*
*
- * - * When a Class is created, the static [jQuery.Class.static.setup setup] + * + * When a Class is created, the static [jQuery.Class.static.setup setup] * and [jQuery.Class.static.init init] methods are called. - * + * * To create an instance of a Class, call: - * + * * new Class([args ... ]) -> instance - * - * The created instance will have all the + * + * The created instance will have all the * prototype properties and methods defined by the PROTOTYPE object. - * - * When an instance is created, the prototype [jQuery.Class.prototype.setup setup] - * and [jQuery.Class.prototype.init init] methods + * + * When an instance is created, the prototype [jQuery.Class.prototype.setup setup] + * and [jQuery.Class.prototype.init init] methods * are called. */ clss = $.Class = function() { if (arguments.length) { - clss.extend.apply(clss, arguments); + return clss.extend.apply(clss, arguments); } }; @@ -358,7 +358,7 @@ steal("jquery","jquery/lang/string",function( $ ) { /** * @function proxy * Returns a callback function for a function on this Class. - * Proxy ensures that 'this' is set appropriately. + * Proxy ensures that 'this' is set appropriately. * @codestart * $.Class("MyClass",{ * getData: function() { @@ -386,7 +386,7 @@ steal("jquery","jquery/lang/string",function( $ ) { * MyClass.getData(showDataFunc) * @codeend *

Nesting Functions

- * Proxy can take an array of functions to call as + * Proxy can take an array of functions to call as * the first argument. When the returned callback function * is called each function in the array is passed the return value of the prior function. This is often used * to eliminate currying initial arguments. @@ -394,7 +394,7 @@ steal("jquery","jquery/lang/string",function( $ ) { * $.Class("MyClass",{ * getData: function( callback ) { * //calls process, then callback with value from process - * $.get("data.json",this.proxy(['process2',callback]),'json') + * $.get("data.json",this.proxy(['process2',callback]),'json') * }, * process2: function( type,jsonData ) { * jsonData.processed = true; @@ -403,7 +403,7 @@ steal("jquery","jquery/lang/string",function( $ ) { * },{}); * MyClass.getData(showDataFunc); * @codeend - * @param {String|Array} fname If a string, it represents the function to be called. + * @param {String|Array} fname If a string, it represents the function to be called. * If it is an array, it will call each function in order and pass the return value of the prior function to the * next function. * @return {Function} the callback function. @@ -421,10 +421,10 @@ steal("jquery","jquery/lang/string",function( $ ) { if (!isArray(funcs) ) { funcs = [funcs]; } - + // keep a reference to us in self self = this; - + //!steal-remove-start for( var i =0; i< funcs.length;i++ ) { if(typeof funcs[i] == "string" && !isFunction(this[funcs[i]])){ @@ -435,27 +435,27 @@ steal("jquery","jquery/lang/string",function( $ ) { return function class_cb() { // add the arguments after the curried args var cur = concatArgs(args, arguments), - isString, + isString, length = funcs.length, f = 0, func; - + // go through each function to call back for (; f < length; f++ ) { func = funcs[f]; if (!func ) { continue; } - + // set called with the name of the function on self (this is how this.view works) isString = typeof func == "string"; if ( isString && self._set_called ) { self.called = func; } - + // call the function cur = (isString ? self[func] : func).apply(self, cur || []); - + // pass the result to the next function (if there is a next function) if ( f < length - 1 ) { cur = !isArray(cur) || cur._use_call ? [cur] : cur @@ -479,7 +479,7 @@ steal("jquery","jquery/lang/string",function( $ ) { // get a raw instance objet (init is not called) var inst = this.rawInstance(), args; - + // call setup if there is a setup if ( inst.setup ) { args = inst.setup.apply(inst, arguments); @@ -493,24 +493,24 @@ steal("jquery","jquery/lang/string",function( $ ) { /** * Setup gets called on the inherting class with the base class followed by the * inheriting class's raw properties. - * - * Setup will deeply extend a static defaults property on the base class with + * + * Setup will deeply extend a static defaults property on the base class with * properties on the base class. For example: - * + * * $.Class("MyBase",{ * defaults : { * foo: 'bar' * } * },{}) - * + * * MyBase("Inheriting",{ * defaults : { * newProp : 'newVal' * } * },{} - * + * * Inheriting.defaults -> {foo: 'bar', 'newProp': 'newVal'} - * + * * @param {Object} baseClass the base class that is being inherited from * @param {String} fullName the name of the new class * @param {Object} staticProps the static properties of the new class @@ -532,28 +532,28 @@ steal("jquery","jquery/lang/string",function( $ ) { /** * Extends a class with new static and prototype functions. There are a variety of ways * to use extend: - * + * * // with className, static and prototype functions * $.Class('Task',{ STATIC },{ PROTOTYPE }) * // with just classname and prototype functions * $.Class('Task',{ PROTOTYPE }) * // with just a className * $.Class('Task') - * + * * You no longer have to use .extend. Instead, you can pass those options directly to * $.Class (and any inheriting classes): - * + * * // with className, static and prototype functions * $.Class('Task',{ STATIC },{ PROTOTYPE }) * // with just classname and prototype functions * $.Class('Task',{ PROTOTYPE }) * // with just a className * $.Class('Task') - * + * * @param {String} [fullName] the classes name (used for classes w/ introspection) * @param {Object} [klass] the new classes static/class functions * @param {Object} [proto] the new classes prototype functions - * + * * @return {jQuery.Class} returns the new class */ extend: function( fullName, klass, proto ) { @@ -578,7 +578,7 @@ steal("jquery","jquery/lang/string",function( $ ) { initializing = true; prototype = new this(); initializing = false; - + // Copy the properties over onto the new prototype inheritProps(proto, _super, prototype); @@ -588,7 +588,7 @@ steal("jquery","jquery/lang/string",function( $ ) { if ( initializing ) return; // we are being called w/o new, we are extending - if ( this.constructor !== Class && arguments.length ) { + if ( this.constructor !== Class && arguments.length ) { return arguments.callee.extend.apply(arguments.callee, arguments) } else { //we are being called w/ new return this.Class.newInstance.apply(this.Class, arguments) @@ -627,33 +627,33 @@ steal("jquery","jquery/lang/string",function( $ ) { extend(Class, { prototype: prototype, /** - * @attribute namespace + * @attribute namespace * The namespaces object - * + * * $.Class("MyOrg.MyClass",{},{}) * MyOrg.MyClass.namespace //-> MyOrg - * + * */ namespace: namespace, /** - * @attribute shortName + * @attribute shortName * The name of the class without its namespace, provided for introspection purposes. - * + * * $.Class("MyOrg.MyClass",{},{}) * MyOrg.MyClass.shortName //-> 'MyClass' * MyOrg.MyClass.fullName //-> 'MyOrg.MyClass' - * + * */ shortName: shortName, constructor: Class, /** - * @attribute fullName + * @attribute fullName * The full name of the class, including namespace, provided for introspection purposes. - * + * * $.Class("MyOrg.MyClass",{},{}) * MyOrg.MyClass.shortName //-> 'MyClass' * MyOrg.MyClass.fullName //-> 'MyOrg.MyClass' - * + * */ fullName: fullName }); @@ -661,11 +661,11 @@ steal("jquery","jquery/lang/string",function( $ ) { //make sure our prototype looks nice Class[STR_PROTOTYPE].Class = Class[STR_PROTOTYPE].constructor = Class; - + // call the class setup var args = Class.setup.apply(Class, concatArgs([_super_class],arguments)); - + // call the class init if ( Class.init ) { Class.init.apply(Class, args || concatArgs([_super_class],arguments)); @@ -673,12 +673,12 @@ steal("jquery","jquery/lang/string",function( $ ) { /* @Prototype*/ return Class; - /** + /** * @function setup - * If a setup method is provided, it is called when a new + * If a setup method is provided, it is called when a new * instances is created. It gets passed the same arguments that * were given to the Class constructor function ( new Class( arguments ... )). - * + * * $.Class("MyClass", * { * setup: function( val ) { @@ -687,43 +687,43 @@ steal("jquery","jquery/lang/string",function( $ ) { * }) * var mc = new MyClass("Check Check") * mc.val //-> 'Check Check' - * - * Setup is called before [jQuery.Class.prototype.init init]. If setup - * return an array, those arguments will be used for init. - * + * + * Setup is called before [jQuery.Class.prototype.init init]. If setup + * return an array, those arguments will be used for init. + * * $.Class("jQuery.Controller",{ * setup : function(htmlElement, rawOptions){ - * return [$(htmlElement), - * $.extend({}, this.Class.defaults, rawOptions )] + * return [$(htmlElement), + * $.extend({}, this.Class.defaults, rawOptions )] * } * }) - * - *
PRO TIP: + * + *
PRO TIP: * Setup functions are used to normalize constructor arguments and provide a place for * setup code that extending classes don't have to remember to call _super to * run. *
- * + * * Setup is not defined on $.Class itself, so calling super in inherting classes * will break. Don't do the following: - * + * * $.Class("Thing",{ * setup : function(){ * this._super(); // breaks! * } * }) - * - * @return {Array|undefined} If an array is return, [jQuery.Class.prototype.init] is + * + * @return {Array|undefined} If an array is return, [jQuery.Class.prototype.init] is * called with those arguments; otherwise, the original arguments are used. */ //break up - /** + /** * @function init * If an init method is provided, it gets called when a new instance - * is created. Init gets called after [jQuery.Class.prototype.setup setup], typically with the - * same arguments passed to the Class - * constructor: ( new Class( arguments ... )). - * + * is created. Init gets called after [jQuery.Class.prototype.setup setup], typically with the + * same arguments passed to the Class + * constructor: ( new Class( arguments ... )). + * * $.Class("MyClass", * { * init: function( val ) { @@ -732,32 +732,32 @@ steal("jquery","jquery/lang/string",function( $ ) { * }) * var mc = new MyClass(1) * mc.val //-> 1 - * + * * [jQuery.Class.prototype.setup Setup] is able to modify the arguments passed to init. Read * about it there. - * + * */ //Breaks up code /** * @attribute constructor - * - * A reference to the Class (or constructor function). This allows you to access + * + * A reference to the Class (or constructor function). This allows you to access * a class's static properties from an instance. - * + * * ### Quick Example - * + * * // a class with a static property * $.Class("MyClass", {staticProperty : true}, {}); - * + * * // a new instance of myClass * var mc1 = new MyClass(); - * + * * // read the static property from the instance: * mc1.constructor.staticProperty //-> true - * + * * Getting static properties with the constructor property, like * [jQuery.Class.static.fullName fullName], is very common. - * + * */ } @@ -770,11 +770,11 @@ steal("jquery","jquery/lang/string",function( $ ) { clss.callback = clss[STR_PROTOTYPE].callback = clss[STR_PROTOTYPE]. /** * @function proxy - * Returns a method that sets 'this' to the current instance. This does the same thing as + * Returns a method that sets 'this' to the current instance. This does the same thing as * and is described better in [jQuery.Class.static.proxy]. * The only difference is this proxy works * on a instance instead of a class. - * @param {String|Array} fname If a string, it represents the function to be called. + * @param {String|Array} fname If a string, it represents the function to be called. * If it is an array, it will call each function in order and pass the return value of the prior function to the * next function. * @return {Function} the callback function