Repository navigation
Expand file tree
/
Copy pathconstruct.js
More file actions
436 lines (407 loc) · 13.9 KB
/
Copy pathconstruct.js
File metadata and controls
436 lines (407 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
//@steal-clean
steal("can/util/string", function(can) {
// ## construct.js
// `can.Construct`
// _This is a modified version of
// [John Resig's class](http://ejohn.org/blog/simple-javascript-inheritance/).
// It provides class level inheritance and callbacks._
// A private flag used to initialize a new class instance without
// initializing it's bindings.
var initializing = 0;
/**
* @add can.Construct
*/
can.Construct = function() {
if (arguments.length) {
return can.Construct.extend.apply(can.Construct, arguments);
}
};
/**
* @static
*/
can.extend(can.Construct, {
/**
* @function newInstance
* Creates a new instance of the constructor function. This method is useful for creating new instances
* with arbitrary parameters. Typically you want to simply use the __new__ operator instead.
*
* ## Example
*
* The following creates a `Person` Construct and then creates a new instance of person, but
* by using `apply` on newInstance to pass arbitrary parameters.
*
* var Person = can.Construct({
* init : function(first, middle, last) {
* this.first = first;
* this.middle = middle;
* this.last = last;
* }
* });
*
* var args = ["Justin","Barry","Meyer"],
* justin = new Person.newInstance.apply(null, args);
*
* @param {Object} [args] arguments that get passed to [can.Construct::setup] and [can.Construct::init]. Note
* that if [can.Construct::setup] returns an array, those arguments will be passed to [can.Construct::init]
* instead.
* @return {class} instance of the class
*/
newInstance: function() {
// Get a raw instance object (`init` is not called).
var inst = this.instance(),
arg = arguments,
args;
// Call `setup` if there is a `setup`
if ( inst.setup ) {
args = inst.setup.apply(inst, arguments);
}
// Call `init` if there is an `init`
// If `setup` returned `args`, use those as the arguments
if ( inst.init ) {
inst.init.apply(inst, args || arguments);
}
return inst;
},
// Overwrites an object with methods. Used in the `super` plugin.
// `newProps` - New properties to add.
// `oldProps` - Where the old properties might be (used with `super`).
// `addTo` - What we are adding to.
_inherit: function( newProps, oldProps, addTo ) {
can.extend(addTo || newProps, newProps || {})
},
// used for overwriting a single property.
// this should be used for patching other objects
// the super plugin overwrites this
_overwrite : function(what, oldProps, propName, val){
what[propName] = val;
},
// Set `defaults` as the merger of the parent `defaults` and this
// object's `defaults`. If you overwrite this method, make sure to
// include option merging logic.
/**
* Setup is called immediately after a constructor function is created and
* set to inherit from its base constructor. It is called with a base constructor and
* the params used to extend the base constructor. It is useful for setting up additional inheritance work.
*
* ## Example
*
* The following creates a `Base` class that when extended, adds a reference to the base
* class.
*
*
* Base = can.Construct({
* setup : function(base, fullName, staticProps, protoProps){
* this.base = base;
* // call base functionality
* can.Construct.setup.apply(this, arguments)
* }
* },{});
*
* Base.base //-> can.Construct
*
* Inherting = Base({});
*
* Inheriting.base //-> Base
*
* ## Base Functionality
*
* Setup deeply extends the static `defaults` property of the base constructor with
* properties of the inheriting constructor. For example:
*
* MyBase = can.Construct({
* defaults : {
* foo: 'bar'
* }
* },{})
*
* Inheriting = MyBase({
* defaults : {
* newProp : 'newVal'
* }
* },{}
*
* Inheriting.defaults // -> {foo: 'bar', 'newProp': 'newVal'}
*
* @param {Object} base the base constructor that is being inherited from
* @param {String} [fullName] the name of the new constructor
* @param {Object} [staticProps] the static properties of the new constructor
* @param {Object} [protoProps] the prototype properties of the new constructor
*/
setup: function( base, fullName ) {
this.defaults = can.extend(true,{}, base.defaults, this.defaults);
},
// Create's a new `class` instance without initializing by setting the
// `initializing` flag.
instance: function() {
// Prevents running `init`.
initializing = 1;
var inst = new this();
// Allow running `init`.
initializing = 0;
return inst;
},
// Extends classes.
/**
* @hide
* Extends a class with new static and prototype functions. There are a variety of ways
* to use extend:
*
* // with className, static and prototype functions
* can.Construct('Task',{ STATIC },{ PROTOTYPE })
* // with just classname and prototype functions
* can.Construct('Task',{ PROTOTYPE })
* // with just a className
* can.Construct('Task')
*
* You no longer have to use <code>.extend</code>. Instead, you can pass those options directly to
* can.Construct (and any inheriting classes):
*
* // with className, static and prototype functions
* can.Construct('Task',{ STATIC },{ PROTOTYPE })
* // with just classname and prototype functions
* can.Construct('Task',{ PROTOTYPE })
* // with just a className
* can.Construct('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 {can.Construct} returns the new class
*/
extend: function( fullName, klass, proto ) {
// Figure out what was passed and normalize it.
if ( typeof fullName != 'string' ) {
proto = klass;
klass = fullName;
fullName = null;
}
if ( ! proto ) {
proto = klass;
klass = null;
}
proto = proto || {};
var _super_class = this,
_super = this.prototype,
name, shortName, namespace, prototype;
// Instantiate a base class (but only create the instance,
// don't run the init constructor).
prototype = this.instance();
// Copy the properties over onto the new prototype.
can.Construct._inherit(proto, _super, prototype);
// The dummy class constructor.
function Constructor() {
// All construction is actually done in the init method.
if ( ! initializing ) {
return this.constructor !== Constructor && arguments.length ?
// We are being called without `new` or we are extending.
arguments.callee.extend.apply(arguments.callee, arguments) :
// We are being called with `new`.
this.constructor.newInstance.apply(this.constructor, arguments);
}
}
// Copy old stuff onto class (can probably be merged w/ inherit)
for ( name in _super_class ) {
if ( _super_class.hasOwnProperty(name) ) {
Constructor[name] = _super_class[name];
}
}
// Copy new static properties on class.
can.Construct._inherit(klass, _super_class, Constructor);
// Setup namespaces.
if ( fullName ) {
var parts = fullName.split('.'),
shortName = parts.pop(),
current = can.getObject(parts.join('.'), window, true),
namespace = current,
_fullName = can.underscore(fullName.replace(/\./g, "_")),
_shortName = can.underscore(shortName);
//!steal-remove-start
if(current[shortName]){
steal.dev.warn("class.js There's already something called "+fullName)
}
//!steal-remove-end
current[shortName] = Constructor;
}
// Set things that shouldn't be overwritten.
can.extend(Constructor, {
constructor: Constructor,
prototype: prototype,
/**
* @attribute namespace
* The namespace keyword is used to declare a scope. This enables you to organize
* code and provides a way to create globally unique types.
*
* can.Construct("MyOrg.MyConstructor",{},{})
* MyOrg.MyConstructor.namespace //-> MyOrg
*
*/
namespace: namespace,
/**
* @attribute shortName
* If you pass a name when creating a Construct, the `shortName` property will be set to the
* actual name without the namespace:
*
* can.Construct("MyOrg.MyConstructor",{},{})
* MyOrg.MyConstructor.shortName //-> 'MyConstructor'
* MyOrg.MyConstructor.fullName //-> 'MyOrg.MyConstructor'
*
*/
shortName: shortName,
_shortName : _shortName,
/**
* @attribute fullName
* If you pass a name when creating a Construct, the `fullName` property will be set to
* the actual name including the full namespace:
*
* can.Construct("MyOrg.MyConstructor",{},{})
* MyOrg.MyConstructor.shortName //-> 'MyConstructor'
* MyOrg.MyConstructor.fullName //-> 'MyOrg.MyConstructor'
*
*/
fullName: fullName,
_fullName: _fullName
});
// Make sure our prototype looks nice.
Constructor.prototype.constructor = Constructor;
// Call the class `setup` and `init`
var t = [_super_class].concat(can.makeArray(arguments)),
args = Constructor.setup.apply(Constructor, t );
if ( Constructor.init ) {
Constructor.init.apply(Constructor, args || t );
}
/**
* @prototype
*/
return Constructor;
/**
* @function setup
*
* If a prototype `setup` method is provided, it is called when a new
* instance is created. It is passed the same arguments that
* were passed to the Constructor constructor
* function (`new Constructor( arguments ... )`). If `setup` returns an
* array, those arguments are passed to [can.Construct::init] instead
* of the original arguments.
*
* Typically, you should only provide [can.Construct::init] methods to
* handle initilization code. Use `setup` for:
*
* - initialization code that you want to run before inheriting constructor's
* init method is called.
* - initialization code that should run without inheriting constructors having to
* call base methods (ex: `MyBase.prototype.init.call(this, arg1)`).
* - passing modified/normalized arguments to `init`.
*
* ## Examples
*
* The following is similar to code in [can.Control]'s setup method that
* converts the first argument to a jQuery collection and extends the
* second argument with the constructor's [can.Construct.defaults defaults]:
*
* can.Construct("can.Control",{
* setup: function( htmlElement, rawOptions ) {
* // set this.element
* this.element = $(htmlElement);
*
* // set this.options
* this.options = can.extend( {},
* this.constructor.defaults,
* rawOptions );
*
* // pass the wrapped element and extended options
* return [this.element, this.options]
* }
* })
*
* ## Base Functionality
*
* Setup is not defined on can.Construct itself, so calling super in inherting classes
* will break. Don't do the following:
*
* Thing = can.Construct({
* setup : function(){
* this._super(); // breaks!
* }
* })
*
* @return {Array|undefined} If an array is return, [can.Construct.prototype.init] is
* called with those arguments; otherwise, the original arguments are used.
*/
//
/**
* @function init
*
* If a prototype `init` method is provided, it gets called after [can.Construct::setup] when a new instance
* is created. The `init` method is where your constructor code should go. Typically,
* you will find it saving the arguments passed to the constructor function for later use.
*
* ## Examples
*
* The following creates a Person constructor with a first and last name property:
*
* var Person = can.Construct({
* init : function(first, last){
* this.first = first;
* this.last = last;
* }
* })
*
* var justin = new Person("Justin","Meyer");
* justin.first //-> "Justin"
* justin.last //-> "Meyer"
*
* The following extends person to create a Programmer constructor
*
* var Programmer = Person({
* init : function(first, last, lang){
* // call base functionality
* Person.prototype.init.call(this, first, last);
*
* // save the lang
* this.lang = lang
* },
* greet : function(){
* return "I am " + this.first + " " + this.last + ". " +
* "I write " + this.lang + ".";
* }
* })
*
* var brian = new Programmer("Brian","Moschel","ECMAScript")
* brian.greet() //-> "I am Brian Moschel.\
* // I write ECMAScript."
*
* ## Notes
*
* [can.Construct::setup] is able to modify the arguments passed to init.
*
* It doesn't matter what init returns because the `new` keyword always
* returns the new object.
*/
//
/**
* @attribute constructor
*
* A reference to the constructor function that created the instance. It allows you to access
* the constructor function's static properties from an instance.
*
* ## Example
*
* Incrementing a static counter, that counts how many instances have been created:
*
* Counter = can.Construct({
* count : 0
* },{
* init : function(){
* this.constructor.count++;
* }
* })
*
* new Counter();
* Counter.count //-> 1;
*
*/
}
});
return can.Construct;
})