/
observe.js
1021 lines (964 loc) · 28.7 KB
/
observe.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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
steal('jquery/class').then(function() {
// Alias helpful methods from jQuery
var isArray = $.isArray,
isObject = function( obj ) {
return typeof obj === 'object' && obj !== null && obj;
},
makeArray = $.makeArray,
each = $.each,
// listens to changes on val and 'bubbles' the event up
// - val the object to listen to changes on
// - prop the property name val is at on
// - parent the parent object of prop
hookup = function( val, prop, parent ) {
// if it's an array make a list, otherwise a val
if (val instanceof $.Observe){
// we have an observe already
// make sure it is not listening to this already
unhookup([val], parent._namespace)
} else if ( isArray(val) ) {
val = new $.Observe.List(val)
} else {
val = new $.Observe(val)
}
// attr (like target, how you (delegate) to get to the target)
// currentAttr (how to get to you)
// delegateAttr (hot to get to the delegated Attr)
//
//
//listen to all changes and trigger upwards
val.bind("change" + parent._namespace, function( ev, attr ) {
// trigger the type on this ...
var args = $.makeArray(arguments),
ev = args.shift();
if(prop === "*"){
args[0] = parent.indexOf(val)+"." + args[0]
} else {
args[0] = prop + "." + args[0]
}
// change the attr
//ev.origTarget = ev.origTarget || ev.target;
// the target should still be the original object ...
$.event.trigger(ev, args, parent)
});
return val;
},
unhookup = function(items, namespace){
var item;
for(var i =0; i < items.length; i++){
item = items[i]
if( item && item.unbind ){
item.unbind("change" + namespace)
}
}
},
// an id to track events for a given observe
id = 0,
collecting = null,
// call to start collecting events (Observe sends all events at once)
collect = function() {
if (!collecting ) {
collecting = [];
return true;
}
},
// creates an event on item, but will not send immediately
// if collecting events
// - item - the item the event should happen on
// - event - the event name ("change")
// - args - an array of arguments
trigger = function( item, event, args ) {
// send no events if initalizing
if (item._init) {
return;
}
if (!collecting ) {
return $.event.trigger(event, args, item, true)
} else {
collecting.push({
t: item,
ev: event,
args: args
})
}
},
// which batch of events this is for, might not want to send multiple
// messages on the same batch. This is mostly for
// event delegation
batchNum = 0,
// sends all pending events
sendCollection = function() {
var len = collecting.length,
items = collecting.slice(0),
cur;
collecting = null;
batchNum ++;
for ( var i = 0; i < len; i++ ) {
cur = items[i];
// batchNum
$.event.trigger({
type: cur.ev,
batchNum : batchNum
}, cur.args, cur.t)
}
},
// a helper used to serialize an Observe or Observe.List where:
// observe - the observable
// how - to serialize with 'attrs' or 'serialize'
// where - to put properties, in a {} or [].
serialize = function( observe, how, where ) {
// go through each property
observe.each(function( name, val ) {
// if the value is an object, and has a attrs or serialize function
where[name] = isObject(val) && typeof val[how] == 'function' ?
// call attrs or serialize to get the original data back
val[how]() :
// otherwise return the value
val
})
return where;
};
/**
* @class jQuery.Observe
* @parent jquerymx.lang
* @test jquery/lang/observe/qunit.html
*
* Observe provides the awesome observable pattern for
* JavaScript Objects and Arrays. It lets you
*
* - Set and remove property or property values on objects and arrays
* - Listen for changes in objects and arrays
* - Work with nested properties
*
* ## Creating an $.Observe
*
* To create an $.Observe, or $.Observe.List, you can simply use
* the `$.O(data)` shortcut like:
*
* var person = $.O({name: 'justin', age: 29}),
* hobbies = $.O(['programming', 'basketball', 'nose picking'])
*
* Depending on the type of data passed to $.O, it will create an instance of either:
*
* - $.Observe, which is used for objects like: `{foo: 'bar'}`, and
* - [jQuery.Observe.List $.Observe.List], which is used for arrays like `['foo','bar']`
*
* $.Observe.List and $.Observe are very similar. In fact,
* $.Observe.List inherits $.Observe and only adds a few extra methods for
* manipulating arrays like [jQuery.Observe.List.prototype.push push]. Go to
* [jQuery.Observe.List $.Observe.List] for more information about $.Observe.List.
*
* You can also create a `new $.Observe` simply by pass it the data you want to observe:
*
* var data = {
* addresses : [
* {
* city: 'Chicago',
* state: 'IL'
* },
* {
* city: 'Boston',
* state : 'MA'
* }
* ],
* name : "Justin Meyer"
* },
* o = new $.Observe(data);
*
* _o_ now represents an observable copy of _data_.
*
* ## Getting and Setting Properties
*
* Use [jQuery.Observe.prototype.attr attr] and [jQuery.Observe.prototype.attr attrs]
* to get and set properties.
*
* For example, you can read the property values of _o_ with
* `observe.attr( name )` like:
*
* // read name
* o.attr('name') //-> Justin Meyer
*
* And set property names of _o_ with
* `observe.attr( name, value )` like:
*
* // update name
* o.attr('name', "Brian Moschel") //-> o
*
* Observe handles nested data. Nested Objects and
* Arrays are converted to $.Observe and
* $.Observe.Lists. This lets you read nested properties
* and use $.Observe methods on them. The following
* updates the second address (Boston) to 'New York':
*
* o.attr('addresses.1').attrs({
* city: 'New York',
* state: 'NY'
* })
*
* `attrs()` can be used to get all properties back from the observe:
*
* o.attrs() // ->
* {
* addresses : [
* {
* city: 'Chicago',
* state: 'IL'
* },
* {
* city: 'New York',
* state : 'MA'
* }
* ],
* name : "Brian Moschel"
* }
*
* ## Listening to property changes
*
* When a property value is changed, it creates events
* that you can listen to. There are two ways to listen
* for events:
*
* - [jQuery.Observe.prototype.bind bind] - listen for any type of change
* - [jQuery.Observe.prototype.delegate delegate] - listen to a specific type of change
*
* With `bind( "change" , handler( ev, attr, how, newVal, oldVal ) )`, you can listen
* to any change that happens within the
* observe. The handler gets called with the property name that was
* changed, how it was changed ['add','remove','set'], the new value
* and the old value.
*
* o.bind('change', function( ev, attr, how, nevVal, oldVal ) {
*
* })
*
* `delegate( attr, event, handler(ev, newVal, oldVal ) )` lets you listen
* to a specific event on a specific attribute.
*
* // listen for name changes
* o.delegate("name","set", function(){
*
* })
*
* Delegate lets you specify multiple attributes and values to match
* for the callback. For example,
*
* r = $.O({type: "video", id : 5})
* r.delegate("type=images id","set", function(){})
*
* This is used heavily by [jQuery.route $.route].
*
* @constructor
*
* @param {Object} obj a JavaScript Object that will be
* converted to an observable
*/
$.Class('jQuery.Observe',
/**
* @prototype
*/
{
init: function( obj ) {
// _data is where we keep the properties
this._data = {};
// the namespace this object uses to listen to events
this._namespace = ".observe" + (++id);
// sets all attrs
this._init = true;
this.attrs(obj);
delete this._init;
},
/**
* Get or set an attribute on the observe.
*
* o = new $.Observe({});
*
* // sets a user property
* o.attr('user',{name: 'hank'});
*
* // read the user's name
* o.attr('user.name') //-> 'hank'
*
* If a value is set for the first time, it will trigger
* an `'add'` and `'set'` change event. Once
* the value has been added. Any future value changes will
* trigger only `'set'` events.
*
*
* @param {String} attr the attribute to read or write.
*
* o.attr('name') //-> reads the name
* o.attr('name', 'Justin') //-> writes the name
*
* You can read or write deep property names. For example:
*
* o.attr('person', {name: 'Justin'})
* o.attr('person.name') //-> 'Justin'
*
* @param {Object} [val] if provided, sets the value.
* @return {Object} the observable or the attribute property.
*
* If you are reading, the property value is returned:
*
* o.attr('name') //-> Justin
*
* If you are writing, the observe is returned for chaining:
*
* o.attr('name',"Brian").attr('name') //-> Justin
*/
attr: function( attr, val ) {
if ( val === undefined ) {
// if we are getting a value
return this._get(attr)
} else {
// otherwise we are setting
this._set(attr, val);
return this;
}
},
/**
* Iterates through each attribute, calling handler
* with each attribute name and value.
*
* new Observe({foo: 'bar'})
* .each(function(name, value){
* equals(name, 'foo')
* equals(value,'bar')
* })
*
* @param {function} handler(attrName,value) A function that will get
* called back with the name and value of each attribute on the observe.
*
* Returning `false` breaks the looping. The following will never
* log 3:
*
* new Observe({a : 1, b : 2, c: 3})
* .each(function(name, value){
* console.log(value)
* if(name == 2){
* return false;
* }
* })
*
* @return {jQuery.Observe} the original observable.
*/
each: function() {
return each.apply(null, [this.__get()].concat(makeArray(arguments)))
},
/**
* Removes a property
*
* o = new $.Observe({foo: 'bar'});
* o.removeAttr('foo'); //-> 'bar'
*
* This creates a `'remove'` change event. Learn more about events
* in [jQuery.Observe.prototype.bind bind] and [jQuery.Observe.prototype.delegate delegate].
*
* @param {String} attr the attribute name to remove.
* @return {Object} the value that was removed.
*/
removeAttr: function( attr ) {
// convert the attr into parts (if nested)
var parts = isArray(attr) ? attr : attr.split("."),
// the actual property to remove
prop = parts.shift(),
// the current value
current = this._data[prop];
// if we have more parts, call removeAttr on that part
if ( parts.length ) {
return current.removeAttr(parts)
} else {
// otherwise, delete
delete this._data[prop];
// create the event
trigger(this, "change", [prop, "remove", undefined, current]);
return current;
}
},
// reads a property from the object
_get: function( attr ) {
var parts = isArray(attr) ? attr : (""+attr).split("."),
current = this.__get(parts.shift());
if ( parts.length ) {
return current ? current._get(parts) : undefined
} else {
return current;
}
},
// reads a property directly if an attr is provided, otherwise
// returns the 'real' data object itself
__get: function( attr ) {
return attr ? this._data[attr] : this._data;
},
// sets attr prop as value on this object where
// attr - is a string of properties or an array of property values
// value - the raw value to set
// description - an object with converters / serializers / defaults / getterSetters?
_set: function( attr, value ) {
// convert attr to attr parts (if it isn't already)
var parts = isArray(attr) ? attr : ("" + attr).split("."),
// the immediate prop we are setting
prop = parts.shift(),
// its current value
current = this.__get(prop);
// if we have an object and remaining parts
if ( isObject(current) && parts.length ) {
// that object should set it (this might need to call attr)
current._set(parts, value)
} else if (!parts.length ) {
// otherwise, we are setting it on this object
// todo: check if value is object and transform
// are we changing the value
if ( value !== current ) {
// check if we are adding this for the first time
// if we are, we need to create an 'add' event
var changeType = this.__get().hasOwnProperty(prop) ? "set" : "add";
// set the value on data
this.__set(prop,
// if we are getting an object
isObject(value) ?
// hook it up to send event to us
hookup(value, prop, this) :
// value is normal
value);
// trigger the change event
trigger(this, "change", [prop, changeType, value, current]);
// if we can stop listening to our old value, do it
current && unhookup([current], this._namespace);
}
} else {
throw "jQuery.Observe: set a property on an object that does not exist"
}
},
// directly sets a property on this object
__set: function( prop, val ) {
this._data[prop] = val;
// add property directly for easy writing
// check if its on the prototype so we don't overwrite methods like attrs
if (!(prop in this.constructor.prototype)) {
this[prop] = val
}
},
/**
* Listens to changes on a jQuery.Observe.
*
* When attributes of an observe change, including attributes on nested objects,
* a `'change'` event is triggered on the observe. These events come
* in three flavors:
*
* - `add` - a attribute is added
* - `set` - an existing attribute's value is changed
* - `remove` - an attribute is removed
*
* The change event is fired with:
*
* - the attribute changed
* - how it was changed
* - the newValue of the attribute
* - the oldValue of the attribute
*
* Example:
*
* o = new $.Observe({name : "Payal"});
* o.bind('change', function(ev, attr, how, newVal, oldVal){
* // ev -> {type: 'change'}
* // attr -> "name"
* // how -> "add"
* // newVal-> "Justin"
* // oldVal-> undefined
* })
*
* o.attr('name', 'Justin')
*
* Listening to `change` is only useful for when you want to
* know every change on an Observe. For most applications,
* [jQuery.Observe.prototype.delegate delegate] is
* much more useful as it lets you listen to specific attribute
* changes and sepecific types of changes.
*
*
* @param {String} eventType the event name. Currently,
* only 'change' events are supported. For more fine
* grained control, use [jQuery.Observe.prototype.delegate].
*
* @param {Function} handler(event, attr, how, newVal, oldVal) A
* callback function where
*
* - event - the event
* - attr - the name of the attribute changed
* - how - how the attribute was changed (add, set, remove)
* - newVal - the new value of the attribute
* - oldVal - the old value of the attribute
*
* @return {$.Observe} the observe for chaining.
*/
bind: function( eventType, handler ) {
$.fn.bind.apply($([this]), arguments);
return this;
},
/**
* Unbinds a listener. This uses [http://api.jquery.com/unbind/ jQuery.unbind]
* and works very similar. This means you can
* use namespaces or unbind all event handlers for a given event:
*
* // unbind a specific event handler
* o.unbind('change', handler)
*
* // unbind all change event handlers bound with the
* // foo namespace
* o.unbind('change.foo')
*
* // unbind all change event handlers
* o.unbind('change')
*
* @param {String} eventType - the type of event with
* any optional namespaces. Currently, only `change` events
* are supported with bind.
*
* @param {Function} [handler] - The original handler function passed
* to [jQuery.Observe.prototype.bind bind].
*
* @return {jQuery.Observe} the original observe for chaining.
*/
unbind: function( eventType, handler ) {
$.fn.unbind.apply($([this]), arguments);
return this;
},
/**
* Get the serialized Object form of the observe. Serialized
* data is typically used to send back to a server.
*
* o.serialize() //-> { name: 'Justin' }
*
* Serialize currently returns the same data
* as [jQuery.Observe.prototype.attrs]. However, in future
* versions, serialize will be able to return serialized
* data similar to [jQuery.Model]. The following will work:
*
* new Observe({time: new Date()})
* .serialize() //-> { time: 1319666613663 }
*
* @return {Object} a JavaScript Object that can be
* serialized with `JSON.stringify` or other methods.
*
*/
serialize: function() {
return serialize(this, 'serialize', {});
},
/**
* Set multiple properties on the observable
* @param {Object} props
* @param {Boolean} remove true if you should remove properties that are not in props
*/
attrs: function( props, remove ) {
if ( props === undefined ) {
return serialize(this, 'attrs', {})
}
props = $.extend(true, {}, props);
var prop, collectingStarted = collect();
for ( prop in this._data ) {
var curVal = this._data[prop],
newVal = props[prop];
// if we are merging ...
if ( newVal === undefined ) {
remove && this.removeAttr(prop);
continue;
}
if ( isObject(curVal) && isObject(newVal) ) {
curVal.attrs(newVal, remove)
} else if ( curVal != newVal ) {
this._set(prop, newVal)
} else {
}
delete props[prop];
}
// add remaining props
for ( var prop in props ) {
newVal = props[prop];
this._set(prop, newVal)
}
if ( collectingStarted ) {
sendCollection();
}
}
});
// Helpers for list
/**
* @class jQuery.Observe.List
* @inherits jQuery.Observe
* @parent jQuery.Observe
*
* An observable list. You can listen to when items are push, popped,
* spliced, shifted, and unshifted on this array.
*
*
*/
var list = jQuery.Observe('jQuery.Observe.List',
/**
* @prototype
*/
{
init: function( instances, options ) {
this.length = 0;
this._namespace = ".list" + (++id);
this._init = true;
this.bind('change',this.proxy('_changes'));
this.push.apply(this, makeArray(instances || []));
$.extend(this, options);
if(this.comparator){
this.sort()
}
delete this._init;
},
_changes : function(ev, attr, how, newVal, oldVal){
// detects an add, sorts it, re-adds?
//console.log("")
// if we are sorting, and an attribute inside us changed
if(this.comparator && /^\d+./.test(attr) ) {
// get the index
var index = +/^\d+/.exec(attr)[0],
// and item
item = this[index],
// and the new item
newIndex = this.sortedIndex(item);
if(newIndex !== index){
// move ...
[].splice.call(this, index, 1);
[].splice.call(this, newIndex, 0, item);
trigger(this, "move", [item, newIndex, index]);
ev.stopImmediatePropagation();
trigger(this,"change", [
attr.replace(/^\d+/,newIndex),
how,
newVal,
oldVal
]);
return;
}
}
// if we add items, we need to handle
// sorting and such
// trigger direct add and remove events ...
if(attr.indexOf('.') === -1){
if( how === 'add' ) {
trigger(this, how, [newVal,+attr]);
} else if( how === 'remove' ) {
trigger(this, how, [oldVal, +attr])
}
}
// issue add, remove, and move events ...
},
sortedIndex : function(item){
var itemCompare = item.attr(this.comparator),
equaled = 0,
i;
for(var i =0; i < this.length; i++){
if(item === this[i]){
equaled = -1;
continue;
}
if(itemCompare <= this[i].attr(this.comparator) ) {
return i+equaled;
}
}
return i+equaled;
},
__get : function(attr){
return attr ? this[attr] : this;
},
__set : function(attr, val){
this[attr] = val;
},
/**
* Returns the serialized form of this list.
*/
serialize: function() {
return serialize(this, 'serialize', []);
},
/**
* Iterates through each item of the list, calling handler
* with each index and value.
*
* new Observe.List(['a'])
* .each(function(index, value){
* equals(index, 1)
* equals(value,'a')
* })
*
* @param {function} handler(index,value) A function that will get
* called back with the index and value of each item on the list.
*
* Returning `false` breaks the looping. The following will never
* log 'c':
*
* new Observe(['a','b','c'])
* .each(function(index, value){
* console.log(value)
* if(index == 1){
* return false;
* }
* })
*
* @return {jQuery.Observe.List} the original observable.
*/
// placeholder for each
/**
* Remove items or add items from a specific point in the list.
*
* ### Example
*
* The following creates a list of numbers and replaces 2 and 3 with
* "a", and "b".
*
* var l = new $.Observe.List([0,1,2,3]);
*
* l.bind('change', function( ev, attr, how, newVals, oldVals, where ) { ... })
*
* l.splice(1,2, "a", "b"); // results in [0,"a","b",3]
*
* This creates 2 change events. The first event is the removal of
* numbers one and two where it's callback values will be:
*
* - attr - "1" - indicates where the remove event took place
* - how - "remove"
* - newVals - undefined
* - oldVals - [1,2] -the array of removed values
* - where - 1 - the location of where these items where removed
*
* The second change event is the addition of the "a", and "b" values where
* the callback values will be:
*
* - attr - "1" - indicates where the add event took place
* - how - "added"
* - newVals - ["a","b"]
* - oldVals - [1, 2] - the array of removed values
* - where - 1 - the location of where these items where added
*
* @param {Number} index where to start removing or adding items
* @param {Object} count the number of items to remove
* @param {Object} [added] an object to add to
*/
splice: function( index, count ) {
var args = makeArray(arguments),
i;
for ( i = 2; i < args.length; i++ ) {
var val = args[i];
if ( isObject(val) ) {
args[i] = hookup(val, "*", this)
}
}
if ( count === undefined ) {
count = args[1] = this.length - index;
}
var removed = [].splice.apply(this, args);
if ( count > 0 ) {
trigger(this, "change", [""+index, "remove", undefined, removed]);
unhookup(removed, this._namespace);
}
if ( args.length > 2 ) {
trigger(this, "change", [""+index, "add", args.slice(2), removed]);
}
return removed;
},
/**
* Updates an array with a new array. It is able to handle
* removes in the middle of the array.
*
* @param {Array} props
* @param {Boolean} remove
*/
attrs: function( props, remove ) {
if ( props === undefined ) {
return serialize(this, 'attrs', []);
}
// copy
props = props.slice(0);
var len = Math.min(props.length, this.length),
collectingStarted = collect();
for ( var prop = 0; prop < len; prop++ ) {
var curVal = this[prop],
newVal = props[prop];
if ( isObject(curVal) && isObject(newVal) ) {
curVal.attrs(newVal, remove)
} else if ( curVal != newVal ) {
this._set(prop, newVal)
} else {
}
}
if ( props.length > this.length ) {
// add in the remaining props
this.push(props.slice(this.length))
} else if ( props.length < this.length && remove ) {
this.splice(props.length)
}
//remove those props didn't get too
if ( collectingStarted ) {
sendCollection()
}
},
sort: function(method, silent){
var comparator = this.comparator,
args = comparator ? [function(a, b){
a = a[comparator]
b = b[comparator]
return a === b ? 0 : (a < b ? -1 : 1);
}] : [],
res = [].sort.apply(this, args);
!silent && trigger(this, "reset");
}
}),
// create push, pop, shift, and unshift
// converts to an array of arguments
getArgs = function( args ) {
if ( args[0] && ($.isArray(args[0])) ) {
return args[0]
}
else {
return makeArray(args)
}
};
// describes the method and where items should be added
each({
/**
* @function push
* Add items to the end of the list.
*
* var l = new $.Observe.List([]);
*
* l.bind('change', function(
* ev, // the change event
* attr, // the attr that was changed, for multiple items, "*" is used
* how, // "add"
* newVals, // an array of new values pushed
* oldVals, // undefined
* where // the location where these items where added
* ) {
*
* })
*
* l.push('0','1','2');
*
* @return {Number} the number of items in the array
*/
push: "length",
/**
* @function unshift
* Add items to the start of the list. This is very similar to
* [jQuery.Observe.prototype.push].
*/
unshift: 0
},
// adds a method where
// - name - method name
// - where - where items in the array should be added
function( name, where ) {
list.prototype[name] = function() {
// get the items being added
var args = getArgs(arguments),
// where we are going to add items
len = where ? this.length : 0;
// go through and convert anything to an observe that needs to be converted
for ( var i = 0; i < args.length; i++ ) {
var val = args[i];
if ( isObject(val) ) {
args[i] = hookup(val, "*", this)
}
}
// if we have a sort item, add that
if( args.length == 1 && this.comparator ) {
// add each item ...
// we could make this check if we are already adding in order
// but that would be confusing ...
var index = this.sortedIndex(args[0]);
this.splice(index, 0, args[0]);
return this.length;
}
// call the original method
var res = [][name].apply(this, args)
// cause the change where the args are:
// len - where the additions happened
// add - items added
// args - the items added
// undefined - the old value
if ( this.comparator && args.length > 1) {
this.sort(null, true);
trigger(this,"reset", [args])
} else {
trigger(this, "change", [""+len, "add", args, undefined])
}
return res;
}
});
each({
/**
* @function pop
*
* Removes an item from the end of the list.
*
* var l = new $.Observe.List([0,1,2]);
*
* l.bind('change', function(
* ev, // the change event
* attr, // the attr that was changed, for multiple items, "*" is used
* how, // "remove"
* newVals, // undefined
* oldVals, // 2
* where // the location where these items where added
* ) {
*
* })
*
* l.pop();
*
* @return {Object} the element at the end of the list
*/
pop: "length",
/**
* @function shift
* Removes an item from the start of the list. This is very similar to
* [jQuery.Observe.prototype.pop].
*
* @return {Object} the element at the start of the list
*/
shift: 0
},
// creates a 'remove' type method
function( name, where ) {
list.prototype[name] = function() {
var args = getArgs(arguments),
len = where && this.length ? this.length - 1 : 0;
var res = [][name].apply(this, args)
// create a change where the args are
// "*" - change on potentially multiple properties
// "remove" - items removed
// undefined - the new values (there are none)
// res - the old, removed values (should these be unbound)
// len - where these items were removed
trigger(this, "change", [""+len, "remove", undefined, [res]])
if ( res && res.unbind ) {
res.unbind("change" + this._namespace)
}
return res;
}
});
list.prototype.
/**