| 1 | define([], function(){ |
| 2 | // TODO: |
| 3 | // allow a declare without a mixin |
| 4 | |
| 5 | return { |
| 6 | // summary: |
| 7 | // Inheritance utilities used in DojoX Drawing |
| 8 | // description: |
| 9 | // Inheritance utilities used in DojoX Drawing. |
| 10 | // There were designed in a effort to make Drawing as |
| 11 | // fast as possible - especially in a case where thousands |
| 12 | // of objects are being loaded. Drawing declare performs |
| 13 | // about 3 times faster than declare and 2 times |
| 14 | // faster than Dojox declare. This is not to say Drawing |
| 15 | // declare is without limitations. It doesn't have the same |
| 16 | // syntactic sugar and extensibility of the other two. You |
| 17 | // can't inherit methods. It won't work with Dijit. But it |
| 18 | // is simple and effective. |
| 19 | |
| 20 | declare: function(){ |
| 21 | // summary: |
| 22 | // Creates a constructor Function from a |
| 23 | // Function, and collection of methods, and |
| 24 | // more Functions that are extended. |
| 25 | // description: |
| 26 | // Similar in look and feel to declare as |
| 27 | // far as order and number of arguments, although |
| 28 | // constructed a little closer to prototypical |
| 29 | // inheritance. All arguments passed into the |
| 30 | // constructor are passed into all sub constructors. |
| 31 | // |
| 32 | // Arguments are: Function, [Object|Function....] |
| 33 | // The first argument is always the base |
| 34 | // constructor. The last argument is always |
| 35 | // an object of methods (or empty object) to |
| 36 | // be mixed in (in the future would like to |
| 37 | // make that object optional). Remaining |
| 38 | // arguments are other constructors mixed in |
| 39 | // using extend() (See below). |
| 40 | // example: |
| 41 | // | MyFunction = dojox.drawing.util.oo.declare( |
| 42 | // | MyOtherFunction, |
| 43 | // | YetAnotherFunction, |
| 44 | // | function(options){ |
| 45 | // | // This is my constructor. It will fire last. |
| 46 | // | // The other constructors will fire before this. |
| 47 | // | }, |
| 48 | // | { |
| 49 | // | customType:"equation", // mixed in property |
| 50 | // | doThing: function(){ // mixed in method |
| 51 | // | |
| 52 | // | } |
| 53 | // | } |
| 54 | // | ); |
| 55 | // | |
| 56 | // | var f = new MyFunction(); |
| 57 | |
| 58 | var f, o, ext=0, a = arguments; |
| 59 | |
| 60 | if(a.length<2){ console.error("drawing.util.oo.declare; not enough arguments")} |
| 61 | if(a.length==2){ |
| 62 | f = a[0]; o = a[1]; |
| 63 | }else{ |
| 64 | a = Array.prototype.slice.call(arguments); |
| 65 | o = a.pop(); |
| 66 | f = a.pop(); |
| 67 | ext = 1; |
| 68 | } |
| 69 | for(var n in o){ |
| 70 | f.prototype[n] = o[n]; |
| 71 | } |
| 72 | if(ext){ |
| 73 | a.unshift(f); |
| 74 | f = this.extend.apply(this, a); |
| 75 | } |
| 76 | return f; // Function |
| 77 | }, |
| 78 | extend: function(){ |
| 79 | // summary: |
| 80 | // Extends constructors to inherit from other |
| 81 | // constructors . |
| 82 | // description: |
| 83 | // Typically not used by itself - it's used as |
| 84 | // part of declare(). Could be used by itself |
| 85 | // however, to mix together two or more |
| 86 | // constructors. |
| 87 | // |
| 88 | // Any number of arguments, all must be |
| 89 | // function constructors. The first is |
| 90 | // considered the base object and its |
| 91 | // constructor will fire first. |
| 92 | // example: |
| 93 | // | var A = function(){}; |
| 94 | // | var B = function(){}; |
| 95 | // | var C = function(){}; |
| 96 | // | var D = dojox.drawing.util.oo.extend(A, B, C); |
| 97 | // | var e = new D(); |
| 98 | |
| 99 | var a = arguments, sub = a[0]; |
| 100 | if(a.length<2){ console.error("drawing.util.oo.extend; not enough arguments")} |
| 101 | var f = function (){ |
| 102 | for(var i=1;i<a.length;i++){ |
| 103 | a[i].prototype.constructor.apply(this, arguments); |
| 104 | } |
| 105 | // sub should fire last |
| 106 | sub.prototype.constructor.apply(this, arguments); |
| 107 | |
| 108 | }; |
| 109 | for(var i=1;i<a.length;i++){ |
| 110 | for(var n in a[i].prototype){ |
| 111 | f.prototype[n] = a[i].prototype[n]; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | for(n in sub.prototype){ |
| 116 | f.prototype[n] = sub.prototype[n]; |
| 117 | } |
| 118 | return f; // Function |
| 119 | } |
| 120 | }; |
| 121 | }); |