31
* (Almost) unmodified John Resig's inheritance
31
* (Almost) unmodified John Resig's inheritance.
33
* Defines The Class object. That object can be used to define and inherit classes using
34
* the extend() method.
38
* var Person = nova.Class.extend({
39
* init: function(isDancing){
40
* this.dancing = isDancing;
43
* return this.dancing;
47
* The init() method act as a constructor. This class can be instancied this way:
49
* var person = new Person(true);
52
* The Person class can also be extended again:
54
* var Ninja = Person.extend({
56
* this._super( false );
59
* // Call the inherited version of dance()
60
* return this._super();
62
* swingSword: function(){
67
* When extending a class, each re-defined method can use this._super() to call the previous
68
* implementation of that method.
34
* Simple JavaScript Inheritance By John Resig http://ejohn.org/ MIT
70
/* Simple JavaScript Inheritance
71
* By John Resig http://ejohn.org/
37
74
// Inspired by base2 and Prototype
39
var initializing = false, fnTest = /xyz/.test(function() {
41
}) ? /\b_super\b/ : /.*/;
42
// The base Class implementation (does nothing)
43
this.Class = function() {
46
// Create a new Class that inherits from this class
47
this.Class.extend = function(prop) {
76
var initializing = false, fnTest = /xyz/.test(function(){xyz;}) ? /\b_super\b/ : /.*/;
77
// The base Class implementation (does nothing)
78
this.Class = function(){};
80
// Create a new Class that inherits from this class
81
this.Class.extend = function(prop) {
48
82
var _super = this.prototype;
50
// Instantiate a web class (but only create the instance,
84
// Instantiate a base class (but only create the instance,
51
85
// don't run the init constructor)
52
86
initializing = true;
53
87
var prototype = new this();
54
88
initializing = false;
56
90
// Copy the properties over onto the new prototype
57
91
for (var name in prop) {
58
// Check if we're overwriting an existing function
59
prototype[name] = typeof prop[name] == "function" &&
60
typeof _super[name] == "function" &&
61
fnTest.test(prop[name]) ?
64
var tmp = this._super;
66
// Add a new ._super() method that is the same
67
// method but on the super-class
68
this._super = _super[name];
70
// The method only need to be bound temporarily, so
71
// we remove it when we're done executing
72
var ret = fn.apply(this, arguments);
77
})(name, prop[name]) :
92
// Check if we're overwriting an existing function
93
prototype[name] = typeof prop[name] == "function" &&
94
typeof _super[name] == "function" && fnTest.test(prop[name]) ?
97
var tmp = this._super;
99
// Add a new ._super() method that is the same method
100
// but on the super-class
101
this._super = _super[name];
103
// The method only need to be bound temporarily, so we
104
// remove it when we're done executing
105
var ret = fn.apply(this, arguments);
110
})(name, prop[name]) :
81
114
// The dummy class constructor
82
115
function Class() {
83
// All construction is actually done in the init method
84
if (!initializing && this.init) {
85
var ret = this.init.apply(this, arguments);
86
if (ret) { return ret; }
116
// All construction is actually done in the init method
117
if ( !initializing && this.init )
118
this.init.apply(this, arguments);
90
Class.include = function (properties) {
91
for (var name in properties) {
92
if (typeof properties[name] !== 'function'
93
|| !fnTest.test(properties[name])) {
94
prototype[name] = properties[name];
95
} else if (typeof prototype[name] === 'function'
96
&& prototype.hasOwnProperty(name)) {
97
prototype[name] = (function (name, fn, previous) {
99
var tmp = this._super;
100
this._super = previous;
101
var ret = fn.apply(this, arguments);
105
})(name, properties[name], prototype[name]);
106
} else if (typeof _super[name] === 'function') {
107
prototype[name] = (function (name, fn) {
109
var tmp = this._super;
110
this._super = _super[name];
111
var ret = fn.apply(this, arguments);
115
})(name, properties[name]);
120
121
// Populate our constructed prototype object
121
122
Class.prototype = prototype;
123
124
// Enforce the constructor to be what we expect
124
Class.constructor = Class;
125
Class.prototype.constructor = Class;
126
127
// And make this class extendable
127
Class.extend = arguments.callee;
129
Class[el] = this[el];
132
135
// end of John Resig's code
138
* Mixin to express the concept of destroying an object.
139
* When an object is destroyed, it should release any resource
140
* it could have reserved before.
134
142
lib.DestroyableMixin = {
135
143
init: function() {
136
144
this.__destroyableDestroyed = false;
147
* Returns true if destroy() was called on the current object.
138
149
isDestroyed : function() {
139
150
return this.__destroyableDestroyed;
153
* Inform the object it should destroy itself, releasing any
154
* resource it could have reserved.
141
156
destroy : function() {
142
157
this.__destroyableDestroyed = true;
162
* Mixin to structure objects' life-cycles folowing a parent-children
163
* relationship. Each object can a have a parent and multiple children.
164
* When an object is destroyed, all its children are destroyed too.
146
166
lib.ParentedMixin = _.extend({}, lib.DestroyableMixin, {
147
167
__parentedMixin : true,
148
168
init: function() {