forked from canjs/canjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
can.js
636 lines (502 loc) · 16.3 KB
/
can.js
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
steal(function(){
/**
@attribute can.VERSION
@parent can.util
`can.VERSION` is a String representing the CanJS version number. A version of `@EDGE`
denotes the bleeding edge CanJS version.
*/
/**
@function can.isDeferred
@parent can.util
`can.isDeferred` returns if an object is an instance of [can.Deferred].
## Example
Convert any value to a Deferred:
function convertDeferred(dfd) {
return can.isDeferred(dfd) ? dfd : can.Deferred(dfd);
}
@param {String} str the string to trim
@return {String} the value of the string
*/
/**
@function can.trim
@parent can.util
`can.trim(string)` removes leading and trailing whitespace from a string. It will also
remove all newlines, spaces including non-breaking, and tabs. If these occur in the middle
of the string, then they will be persisted.
can.trim( " foo " ) // -> "foo"
@param {String} str the string to trim
@return {String} the value of the string
*/
//
/**
@function can.makeArray
@parent can.util
`can.makeArray(object)` convert an array-like object into a array.
can.makeArray({0 : "zero", 1: "one", length: 2})
// -> ["zero","one"]
@param {Object} object to transform into array
@return {Array} converted array
*/
//
/**
@function can.isArray
@parent can.util
`can.array(object)` returns if the object is an Array.
can.isArray([]) //-> true
can.isArray(false)
@param {Array} arr any JS object
@return {Boolean} true if an array
*/
//
/**
@function can.each
@parent can.util
`can.each(object, function)` iterates through an array or object like
like [http://api.jquery.com/jQuery.each/ jQuery.each].
can.each([{prop: "val1"}, {prop: "val2"}],
function( value, index ) {
// function called with
// index=0 value={prop: "val1"}
// index=1 value={prop: "val2"}
})
@param {Object} arr any JS object or array
@return {Object} the function passed to can.each
*/
//
/**
@function can.extend
@parent can.util
`can.extend(target, objectN)` merges the contents of two or more objects together into the first object
similarly to [http://api.jquery.com/jQuery.extend/ jQuery.extend].
var first = {},
second = {a: "b"},
thrid = {c: "d"};
can.extend(first, second, third); //-> first
first //-> {a: "b",c : "d"}
second //-> {a: "b"}
thrid //-> {c: "d"}
@param {Object} target The target object to extend
@param {Object} [object1] An object containing properties to merge
@param {Object} [objectN] Additional objects containing properties to merge
@return {Object} The target object
*/
//
/**
@function can.param
@parent can.util
Parameterizes an object into a query string
like [http://api.jquery.com/jQuery.param/ jQuery.param].
can.param({a: "b", c: "d"}) //-> "a=b&c=d"
@param {Object} obj An array or object to serialize
@return {String} The serialized object
*/
//
/**
@function can.isEmptyObject
@parent can.util
`can.isEmptyObject(object)` returns if an object has no properties similar to
[http://api.jquery.com/jQuery.isEmptyObject/ jQuery.isEmptyObject].
can.isEmptyObject({}) //-> true
can.isEmptyObject({a:"b"}) //-> false
@param {Object} object to evaluate if empty or not
@param {Boolean} Whether the object is empty
*/
//
/**
@function can.proxy
@parent can.util
`can.proxy(function)` accepts a function and returns a
new one that will always the context from which it was
called. This works similar to [http://api.jquery.com/jQuery.proxy/ jQuery.proxy].
var func = can.proxy(function(one){
return this.a + one
}, {a: "b"});
func("two") //-> "btwo"
@param {Function} function to return in the same context
@param {Object} context The context for the new function
@return {Function} The new function
*/
//
/**
@function can.isFunction
@parent can.util
`can.isFunction(object)` returns if an object is a function similar to
[http://api.jquery.com/jQuery.isFunction/ jQuery.isFunction].
can.isFunction({}) //-> false
can.isFunction(function(){}) //-> true
@param {Object} object to evaluate if is function
@return {Boolean} Whether the object is a function
*/
//
/**
@function can.bind
@parent can.util
`can.bind(eventName, handler)` binds a callback handler
on an object for a given event. It works on:
- HTML elements and the window
- Objects
- Objects with bind / unbind methods
The idea is that bind can be used on anything that produces events
and it will figure out the appropriate way to
bind to it. Typically, `can.bind` is only used internally to
CanJS; however, if you are making libraries or extensions, use
`can.bind` to listen to events independent of the underlying library.
__Binding to an object__
var obj = {};
can.bind.call(obj,"something", function(ev, arg1, arg){
arg1 // 1
arg2 // 2
})
can.trigger(obj,"something",[1,2])
__Binding to an HTMLElement__
var el = document.getElementById('foo')
can.bind.call(el, "click", function(ev){
this // el
})
@param {String} eventName The type of event to bind to
@param {Function} handler The handler for the event
@return {Object} this
*/
//
/**
@function can.unbind
@parent can.util
`can.unbind(eventName, handler)` unbinds a callback handler
from an object for a given event. It works on:
- HTML elements and the window
- Objects
- Objects with bind / unbind methods
The idea is that unbind can be used on anything that produces events
and it will figure out the appropriate way to
unbind to it. Typically, `can.unbind` is only used internally to
CanJS; however, if you are making libraries or extensions, use
`can.bind` to listen to events independent of the underlying library.
__Binding/unbinding to an object__
var obj = {},
handler = function(ev, arg1, arg){
arg1 // 1
arg2 // 2
};
can.bind.call(obj,"something", handler)
can.trigger(obj,"something",[1,2])
can.unbind.call(obj,"something", handler)
__Binding/unbinding to an HTMLElement__
var el = document.getElementById('foo'),
handler = function(ev){
this // el
};
can.bind.call(el, "click", handler)
can.unbind.call(el, "click", handler)
@param {String} eventName The type of event to unbind from
@param {Function} handler The handler for the event
@return {Object} this
*/
//
/**
@function can.delegate
@parent can.util
`can.delegate(selector, eventName, handler)` binds a delegate handler
on an object for a given event. It works on:
- HTML elements and the window
The idea is that delegate can be used on anything that produces delegate events
and it will figure out the appropriate way to
bind to it. Typically, `can.delegate` is only used internally to
CanJS; however, if you are making libraries or extensions, use
`can.delegate` to listen to events independent of the underlying library.
__Delegate binding to an HTMLElement__
var el = document.getElementById('foo')
can.delegate.call(el, ".selector", "click", function(ev){
this // el
})
@param {String} selector The selector to delegate
@param {String} eventName The type of event to bind to
@param {Function} handler The handler for the event
@return {Object} this
*/
//
/**
@function can.undelegate
@parent can.util
`can.undelegate(selector, eventName, handler)` unbinds a delegate handler
on an object for a given event. It works on:
- HTML elements and the window
The idea is that undelegate can be used on anything that produces delegate events
and it will figure out the appropriate way to
bind to it. Typically, `can.undelegate` is only used internally to
CanJS; however, if you are making libraries or extensions, use
`can.undelegate` to listen to events independent of the underlying library.
__Delegate/undelegate binding to an HTMLElement__
var el = document.getElementById('foo'),
handler = function(ev){
this // el
};
can.delegate.call(el, ".selector", "click", handler)
can.undelegate.call(el, ".selector", "click", handler)
@param {String} selector The selector to undelegate
@param {String} eventName The type of event to unbind from
@param {Function} handler The handler for the event
@return {Object} this
*/
//
/**
@function can.trigger
@parent can.util
Trigger an event on an element or object.
@param {can.$|Object} obj The object to trigger the event on
@param {String} event The event to trigger
@param {Array} [args] The event data
*/
//
/**
@function can.ajax
@parent can.util
`can.ajax( settings )` is used to make an asynchronous HTTP (Ajax) request
similar to [http://api.jquery.com/jQuery.ajax/ jQuery.ajax].
can.ajax({
url: 'ajax/farm/animals',
success: function(animals) {
can.$('.farm').html(animals);
}
});
@param {Object} options Ajax request configuration options
@return {Deferred}
*/
//
/**
@function can.$
@parent can.util
`can.$(selector|element|elements)` returns the the underlying
library's NodeList. It can be passed
a css selector, a HTMLElement or an array of HTMLElements.
The following lists how the NodeList is created by each library:
- __jQuery__ `jQuery( HTMLElement )`
- __Zepto__ `Zepto( HTMLElement )`
- __Dojo__ `new dojo.NodeList( HTMLElement )`
- __Mootools__ `$$( HTMLElement )`
- __YUI__ `Y.all(selector)` or `Y.NodeList`
@param {String|Element|NodeList} selector The selector to pass to the underlying library
@return {NodeList}
*/
//
/**
@function can.buildFragment
@parent can.util
`can.buildFragment(html, node)` returns a document fragment for the HTML passed.
@param {String} html HTML strings
@param {Array} node element used for accessing the ownerDocument
@return {DocumentFragment}
*/
//
/**
@function can.append
@parent can.util
`can.append( wrappedNodeList, html )` inserts content to the end of each wrapped node list item(s) passed.
// Before
<div id="demo" />
can.append( can.$('#demo'), 'Demos are fun!' );
// After
<div id="demo">Demos are fun!</div>
@param {Object} wrappedNodeList
@param {String} html string to append
*/
//
/**
@function can.remove
@parent can.util
`can.remove( wrappedNodeList )` removes the set of matched element(s) from the DOM.
<div id="wrap"/>
can.remove(can.$('#wrap')) //-> removes 'wrap'
@param {Object} wrappedNodeList of elements to remove from dom.
*/
//
/**
@function can.data
@parent can.util
`can.data` enables the associatation of arbitrary data with DOM nodes and JavaScript objects.
### Setting Data
can.data( can.$('#elm'), key, value )
- __wrappedNodeList__ node list to associate data to.
- __key__ string name of the association.
- __value__ tdata value; it can be any Javascript type including Array or Object.
### Accessing Data
can.data( can.$('#elm'), key )
- __wrappedNodeList__ node list to retrieve association data from.
- __key__ string name of the association.
Due to the way browsers security restrictions with plugins and external code,
the _data_ method cannot be used on `object` (unless it's a Flash plugin), `applet` or `embed` elements.
@param {NodeList} wrapped The wrapped node list to associate data with
@param {String} key The data property to access
@param {Object} [value] The data value to store
@return {Object} The value for the given key
*/
//
/**
@function can.addClass
@parent can.util
`can.addClass( nodelist, className )` adds the specified class(es) to
nodelist's HTMLElements. It does NOT replace any existing class(es)
already defined.
// Before
<div id="foo" class="monkey" />
can.addClass(can.$("#foo"),"bar")
// After
<div id="foo" class="monkey bar" />
You can also pass multiple class(es) and it will add them to the existing
set also.
// Before
<div id="foo" class="monkey" />
can.addClass(can.$("#foo"),"bar man")
// After
<div id="foo" class="monkey bar man" />
This works similarly to [http://api.jquery.com/addClass/ jQuery.fn.addClass].
@param {String} class name to add to the wrapped node list
*/
//
/**
@function can.when
@parent can.util
`can.when(args)` provides the ability to execute callback function(s)
typically based on a Deferred or AJAX object.
can.when( can.ajax('api/farm/animals') ).then(function(animals){
alert(animals); //-> alerts the ajax response
});
You can also use this for regular JavaScript objects.
$.when( { animals: [ 'cat' ] } ).done(function(animals){
alert(animals[0]); //-> alerts 'cat'
});
@param {Object} deferreds ajax or JavaScript objects
*/
//
/**
@class can.Deferred
@parent can.util
`can.Deferred` is a object that allows users to assign and chain callback
function(s) for the success or failure state of both asynchronous and synchronous function(s).
*/
//
/*
* @prototype
*/
//
/**
@function pipe
`deferred.pipe(done, fail)` is a utility to filter Deferred(s).
var def = can.Deferred(),
filtered = def.pipe(function(val) {
return val + " is awesome!";
});
def.resolve('Can');
filtered.done(function(value) {
alert(value); // Alerts: 'Can is awesome!'
});
@param {Object} doneCallbacks A function called when the Deferred is resolved.
@param {Object} failCallbacks A function called when the Deferred is rejected.
*/
//
/**
@function resolveWith
`deferred.resolveWith(context, doneCallbacks)` resolves a Deferred and calls the `doneCallbacks` give the arguments.
var def = can.Deferred();
def.resolveWith(this, { animals: [ 'cows', 'monkey', 'panda' ] })
@param {Object} context Context passed to the `doneCallbacks` as the `this` object.
@param {Object} args Optional array of args that are passed to the `doneCallbacks`.
*/
//
/**
@function rejectWith
`deferred.rejectWith(context, failCallbacks)` rejects a Deferred and calls the `failCallbacks` give the arguments.
var def = can.Deferred();
def.rejectWith(this, { error: "Animals are gone." })
@param {Object} context Context passed to the `doneCallbacks` as the `this` object.
@param {Object} args Optional array of args that are passed to the `failCallbacks`.
*/
//
/**
@function done
`deferred.done(successCallbacks)` adds handler(s) to be called when the Deferred object is resolved.
var def = can.Deferred();
def.done(function(){
//- Deferred is done.
});
@param {Object} successCallbacks function that is called when the Deferred is resolved.
*/
/**
* @function fail
`deferred.fail(successCallbacks)` adds handler(s) to be called when the Deferred object is rejected.
var def = can.Deferred();
def.fail(function(){
//- Deferred got rejected.
});
*/
//
/**
@function always
`deferred.always( alwaysCallbacks )` adds handler(s) to be called when the Deferred object is either resolved or rejected.
var def = can.Deferred();
def.always( function(){
//- Called whether the handler fails or is success.
});
@param {Object} alwaysCallbacks A function called when the Deferred is resolved or rejected.
*/
//
/**
@function then
`deferred.then( doneCallbacks, failCallbacks )` adds handler(s) to be called when the Deferred object to be called after its resolved.
var def = can.Deferred();
def.then(function(){
//- Called when the deferred is resolved.
}, function(){
//- Called when the deferred fails.
})
@param {Object} doneCallbacks A function called when the Deferred is resolved.
@param {Object} failCallbacks A function called when the Deferred is rejected.
*/
//
/**
@function isResolved
`deferred.isResolved()` returns whether a Deferred object has been resolved.
var def = can.Deferred();
var resolved = def.isResolved();
*/
/**
@function isRejected
`deferred.isRejected()` returns whether a Deferred object has been rejected.
var def = can.Deferred();
var rejected = def.isRejected()
*/
//
/**
@function reject
`deferred.reject( args )` rejects the Deferred object and calls the fail callbacks with the given arguments.
var def = can.Deferred();
def.reject({ error: 'Thats not an animal.' })
@param {Object} arguments Optional arguments that are passed to the fail callbacks.
*/
//
/**
@function resolve
`deferred.resolve( args )` resolves a Deferred object and calls the done callbacks with the given arguments.
var def = can.Deferred();
def.resolve({ animals: [ 'pig', 'cow' ] })
@param {Object} arguments Optional arguments that are passed to the done callbacks.
*/
var can = window.can || {};
if(typeof GLOBALCAN === 'undefined' || GLOBALCAN !== false) {
window.can = can;
}
can.isDeferred = function( obj ) {
var isFunction = this.isFunction;
// Returns `true` if something looks like a deferred.
return obj && isFunction(obj.then) && isFunction(obj.pipe);
};
var cid = 0;
can.cid = function(object, name){
if(object._cid){
return object._cid
} else{
return object._cid = (name ||"" ) + (++cid)
}
}
can.VERSION = '@EDGE';
return can;
});