/
runtime.js
5438 lines (4230 loc) · 154 KB
/
runtime.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
//
// This file is automatically generated. any changes will be lost
//
require("./metal");
require("./rsvp");
(function() {
/*globals ENV */
/**
@module ember
@submodule ember-runtime
*/
var indexOf = Ember.EnumerableUtils.indexOf;
// ........................................
// TYPING & ARRAY MESSAGING
//
var TYPE_MAP = {};
var t = "Boolean Number String Function Array Date RegExp Object".split(" ");
Ember.ArrayPolyfills.forEach.call(t, function(name) {
TYPE_MAP[ "[object " + name + "]" ] = name.toLowerCase();
});
var toString = Object.prototype.toString;
/**
Returns a consistent type for the passed item.
Use this instead of the built-in `typeof` to get the type of an item.
It will return the same result across all browsers and includes a bit
more detail. Here is what will be returned:
| Return Value | Meaning |
|---------------|------------------------------------------------------|
| 'string' | String primitive |
| 'number' | Number primitive |
| 'boolean' | Boolean primitive |
| 'null' | Null value |
| 'undefined' | Undefined value |
| 'function' | A function |
| 'array' | An instance of Array |
| 'class' | A Ember class (created using Ember.Object.extend()) |
| 'instance' | A Ember object instance |
| 'error' | An instance of the Error object |
| 'object' | A JavaScript object not inheriting from Ember.Object |
Examples:
Ember.typeOf(); => 'undefined'
Ember.typeOf(null); => 'null'
Ember.typeOf(undefined); => 'undefined'
Ember.typeOf('michael'); => 'string'
Ember.typeOf(101); => 'number'
Ember.typeOf(true); => 'boolean'
Ember.typeOf(Ember.makeArray); => 'function'
Ember.typeOf([1,2,90]); => 'array'
Ember.typeOf(Ember.Object.extend()); => 'class'
Ember.typeOf(Ember.Object.create()); => 'instance'
Ember.typeOf(new Error('teamocil')); => 'error'
// "normal" JavaScript object
Ember.typeOf({a: 'b'}); => 'object'
@method typeOf
@for Ember
@param item {Object} the item to check
@return {String} the type
*/
Ember.typeOf = function(item) {
var ret;
ret = (item === null || item === undefined) ? String(item) : TYPE_MAP[toString.call(item)] || 'object';
if (ret === 'function') {
if (Ember.Object && Ember.Object.detect(item)) ret = 'class';
} else if (ret === 'object') {
if (item instanceof Error) ret = 'error';
else if (Ember.Object && item instanceof Ember.Object) ret = 'instance';
else ret = 'object';
}
return ret;
};
/**
Returns true if the passed value is null or undefined. This avoids errors
from JSLint complaining about use of ==, which can be technically
confusing.
Ember.none(); => true
Ember.none(null); => true
Ember.none(undefined); => true
Ember.none(''); => false
Ember.none([]); => false
Ember.none(function(){}); => false
@method none
@for Ember
@param {Object} obj Value to test
@return {Boolean}
*/
Ember.none = function(obj) {
return obj === null || obj === undefined;
};
/**
Verifies that a value is null or an empty string | array | function.
Constrains the rules on `Ember.none` by returning false for empty
string and empty arrays.
Ember.empty(); => true
Ember.empty(null); => true
Ember.empty(undefined); => true
Ember.empty(''); => true
Ember.empty([]); => true
Ember.empty('tobias fünke'); => false
Ember.empty([0,1,2]); => false
@method empty
@for Ember
@param {Object} obj Value to test
@return {Boolean}
*/
Ember.empty = function(obj) {
return obj === null || obj === undefined || (obj.length === 0 && typeof obj !== 'function') || (typeof obj === 'object' && Ember.get(obj, 'length') === 0);
};
/**
This will compare two javascript values of possibly different types.
It will tell you which one is greater than the other by returning:
- -1 if the first is smaller than the second,
- 0 if both are equal,
- 1 if the first is greater than the second.
The order is calculated based on Ember.ORDER_DEFINITION, if types are different.
In case they have the same type an appropriate comparison for this type is made.
Ember.compare('hello', 'hello'); => 0
Ember.compare('abc', 'dfg'); => -1
Ember.compare(2, 1); => 1
@method compare
@for Ember
@param {Object} v First value to compare
@param {Object} w Second value to compare
@return {Number} -1 if v < w, 0 if v = w and 1 if v > w.
*/
Ember.compare = function compare(v, w) {
if (v === w) { return 0; }
var type1 = Ember.typeOf(v);
var type2 = Ember.typeOf(w);
var Comparable = Ember.Comparable;
if (Comparable) {
if (type1==='instance' && Comparable.detect(v.constructor)) {
return v.constructor.compare(v, w);
}
if (type2 === 'instance' && Comparable.detect(w.constructor)) {
return 1-w.constructor.compare(w, v);
}
}
// If we haven't yet generated a reverse-mapping of Ember.ORDER_DEFINITION,
// do so now.
var mapping = Ember.ORDER_DEFINITION_MAPPING;
if (!mapping) {
var order = Ember.ORDER_DEFINITION;
mapping = Ember.ORDER_DEFINITION_MAPPING = {};
var idx, len;
for (idx = 0, len = order.length; idx < len; ++idx) {
mapping[order[idx]] = idx;
}
// We no longer need Ember.ORDER_DEFINITION.
delete Ember.ORDER_DEFINITION;
}
var type1Index = mapping[type1];
var type2Index = mapping[type2];
if (type1Index < type2Index) { return -1; }
if (type1Index > type2Index) { return 1; }
// types are equal - so we have to check values now
switch (type1) {
case 'boolean':
case 'number':
if (v < w) { return -1; }
if (v > w) { return 1; }
return 0;
case 'string':
var comp = v.localeCompare(w);
if (comp < 0) { return -1; }
if (comp > 0) { return 1; }
return 0;
case 'array':
var vLen = v.length;
var wLen = w.length;
var l = Math.min(vLen, wLen);
var r = 0;
var i = 0;
while (r === 0 && i < l) {
r = compare(v[i],w[i]);
i++;
}
if (r !== 0) { return r; }
// all elements are equal now
// shorter array should be ordered first
if (vLen < wLen) { return -1; }
if (vLen > wLen) { return 1; }
// arrays are equal now
return 0;
case 'instance':
if (Ember.Comparable && Ember.Comparable.detect(v)) {
return v.compare(v, w);
}
return 0;
case 'date':
var vNum = v.getTime();
var wNum = w.getTime();
if (vNum < wNum) { return -1; }
if (vNum > wNum) { return 1; }
return 0;
default:
return 0;
}
};
function _copy(obj, deep, seen, copies) {
var ret, loc, key;
// primitive data types are immutable, just return them.
if ('object' !== typeof obj || obj===null) return obj;
// avoid cyclical loops
if (deep && (loc=indexOf(seen, obj))>=0) return copies[loc];
Ember.assert('Cannot clone an Ember.Object that does not implement Ember.Copyable', !(obj instanceof Ember.Object) || (Ember.Copyable && Ember.Copyable.detect(obj)));
// IMPORTANT: this specific test will detect a native array only. Any other
// object will need to implement Copyable.
if (Ember.typeOf(obj) === 'array') {
ret = obj.slice();
if (deep) {
loc = ret.length;
while(--loc>=0) ret[loc] = _copy(ret[loc], deep, seen, copies);
}
} else if (Ember.Copyable && Ember.Copyable.detect(obj)) {
ret = obj.copy(deep, seen, copies);
} else {
ret = {};
for(key in obj) {
if (!obj.hasOwnProperty(key)) continue;
ret[key] = deep ? _copy(obj[key], deep, seen, copies) : obj[key];
}
}
if (deep) {
seen.push(obj);
copies.push(ret);
}
return ret;
}
/**
Creates a clone of the passed object. This function can take just about
any type of object and create a clone of it, including primitive values
(which are not actually cloned because they are immutable).
If the passed object implements the clone() method, then this function
will simply call that method and return the result.
@method copy
@for Ember
@param {Object} object The object to clone
@param {Boolean} deep If true, a deep copy of the object is made
@return {Object} The cloned object
*/
Ember.copy = function(obj, deep) {
// fast paths
if ('object' !== typeof obj || obj===null) return obj; // can't copy primitives
if (Ember.Copyable && Ember.Copyable.detect(obj)) return obj.copy(deep);
return _copy(obj, deep, deep ? [] : null, deep ? [] : null);
};
/**
Convenience method to inspect an object. This method will attempt to
convert the object into a useful string description.
@method inspect
@for Ember
@param {Object} obj The object you want to inspect.
@return {String} A description of the object
*/
Ember.inspect = function(obj) {
var v, ret = [];
for(var key in obj) {
if (obj.hasOwnProperty(key)) {
v = obj[key];
if (v === 'toString') { continue; } // ignore useless items
if (Ember.typeOf(v) === 'function') { v = "function() { ... }"; }
ret.push(key + ": " + v);
}
}
return "{" + ret.join(" , ") + "}";
};
/**
Compares two objects, returning true if they are logically equal. This is
a deeper comparison than a simple triple equal. For sets it will compare the
internal objects. For any other object that implements `isEqual()` it will
respect that method.
Ember.isEqual('hello', 'hello'); => true
Ember.isEqual(1, 2); => false
Ember.isEqual([4,2], [4,2]); => false
@method isEqual
@for Ember
@param {Object} a first object to compare
@param {Object} b second object to compare
@return {Boolean}
*/
Ember.isEqual = function(a, b) {
if (a && 'function'===typeof a.isEqual) return a.isEqual(b);
return a === b;
};
// Used by Ember.compare
Ember.ORDER_DEFINITION = Ember.ENV.ORDER_DEFINITION || [
'undefined',
'null',
'boolean',
'number',
'string',
'array',
'object',
'instance',
'function',
'class',
'date'
];
/**
Returns all of the keys defined on an object or hash. This is useful
when inspecting objects for debugging. On browsers that support it, this
uses the native Object.keys implementation.
@method keys
@for Ember
@param {Object} obj
@return {Array} Array containing keys of obj
*/
Ember.keys = Object.keys;
if (!Ember.keys) {
Ember.keys = function(obj) {
var ret = [];
for(var key in obj) {
if (obj.hasOwnProperty(key)) { ret.push(key); }
}
return ret;
};
}
// ..........................................................
// ERROR
//
var errorProps = ['description', 'fileName', 'lineNumber', 'message', 'name', 'number', 'stack'];
/**
A subclass of the JavaScript Error object for use in Ember.
@class Error
@namespace Ember
@extends Error
@constructor
*/
Ember.Error = function() {
var tmp = Error.prototype.constructor.apply(this, arguments);
// Unfortunately errors are not enumerable in Chrome (at least), so `for prop in tmp` doesn't work.
for (var idx = 0; idx < errorProps.length; idx++) {
this[errorProps[idx]] = tmp[errorProps[idx]];
}
};
Ember.Error.prototype = Ember.create(Error.prototype);
})();
(function() {
/**
@module ember
@submodule ember-runtime
*/
var STRING_DASHERIZE_REGEXP = (/[ _]/g);
var STRING_DASHERIZE_CACHE = {};
var STRING_DECAMELIZE_REGEXP = (/([a-z])([A-Z])/g);
var STRING_CAMELIZE_REGEXP = (/(\-|_|\s)+(.)?/g);
var STRING_UNDERSCORE_REGEXP_1 = (/([a-z\d])([A-Z]+)/g);
var STRING_UNDERSCORE_REGEXP_2 = (/\-|\s+/g);
/**
Defines the hash of localized strings for the current language. Used by
the `Ember.String.loc()` helper. To localize, add string values to this
hash.
@property STRINGS
@for Ember
@type Hash
*/
Ember.STRINGS = {};
/**
Defines string helper methods including string formatting and localization.
Unless Ember.EXTEND_PROTOTYPES.String is false these methods will also be added
to the String.prototype as well.
@class String
@namespace Ember
@static
*/
Ember.String = {
/**
Apply formatting options to the string. This will look for occurrences
of %@ in your string and substitute them with the arguments you pass into
this method. If you want to control the specific order of replacement,
you can add a number after the key as well to indicate which argument
you want to insert.
Ordered insertions are most useful when building loc strings where values
you need to insert may appear in different orders.
"Hello %@ %@".fmt('John', 'Doe') => "Hello John Doe"
"Hello %@2, %@1".fmt('John', 'Doe') => "Hello Doe, John"
@method fmt
@param {Object...} [args]
@return {String} formatted string
*/
fmt: function(str, formats) {
// first, replace any ORDERED replacements.
var idx = 0; // the current index for non-numerical replacements
return str.replace(/%@([0-9]+)?/g, function(s, argIndex) {
argIndex = (argIndex) ? parseInt(argIndex,0) - 1 : idx++ ;
s = formats[argIndex];
return ((s === null) ? '(null)' : (s === undefined) ? '' : s).toString();
}) ;
},
/**
Formats the passed string, but first looks up the string in the localized
strings hash. This is a convenient way to localize text. See
`Ember.String.fmt()` for more information on formatting.
Note that it is traditional but not required to prefix localized string
keys with an underscore or other character so you can easily identify
localized strings.
Ember.STRINGS = {
'_Hello World': 'Bonjour le monde',
'_Hello %@ %@': 'Bonjour %@ %@'
};
Ember.String.loc("_Hello World");
=> 'Bonjour le monde';
Ember.String.loc("_Hello %@ %@", ["John", "Smith"]);
=> "Bonjour John Smith";
@method loc
@param {String} str The string to format
@param {Array} formats Optional array of parameters to interpolate into string.
@return {String} formatted string
*/
loc: function(str, formats) {
str = Ember.STRINGS[str] || str;
return Ember.String.fmt(str, formats) ;
},
/**
Splits a string into separate units separated by spaces, eliminating any
empty strings in the process. This is a convenience method for split that
is mostly useful when applied to the String.prototype.
Ember.String.w("alpha beta gamma").forEach(function(key) {
console.log(key);
});
> alpha
> beta
> gamma
@method w
@param {String} str The string to split
@return {String} split string
*/
w: function(str) { return str.split(/\s+/); },
/**
Converts a camelized string into all lower case separated by underscores.
'innerHTML'.decamelize() => 'inner_html'
'action_name'.decamelize() => 'action_name'
'css-class-name'.decamelize() => 'css-class-name'
'my favorite items'.decamelize() => 'my favorite items'
@method decamelize
@param {String} str The string to decamelize.
@return {String} the decamelized string.
*/
decamelize: function(str) {
return str.replace(STRING_DECAMELIZE_REGEXP, '$1_$2').toLowerCase();
},
/**
Replaces underscores or spaces with dashes.
'innerHTML'.dasherize() => 'inner-html'
'action_name'.dasherize() => 'action-name'
'css-class-name'.dasherize() => 'css-class-name'
'my favorite items'.dasherize() => 'my-favorite-items'
@method dasherize
@param {String} str The string to dasherize.
@return {String} the dasherized string.
*/
dasherize: function(str) {
var cache = STRING_DASHERIZE_CACHE,
ret = cache[str];
if (ret) {
return ret;
} else {
ret = Ember.String.decamelize(str).replace(STRING_DASHERIZE_REGEXP,'-');
cache[str] = ret;
}
return ret;
},
/**
Returns the lowerCaseCamel form of a string.
'innerHTML'.camelize() => 'innerHTML'
'action_name'.camelize() => 'actionName'
'css-class-name'.camelize() => 'cssClassName'
'my favorite items'.camelize() => 'myFavoriteItems'
@method camelize
@param {String} str The string to camelize.
@return {String} the camelized string.
*/
camelize: function(str) {
return str.replace(STRING_CAMELIZE_REGEXP, function(match, separator, chr) {
return chr ? chr.toUpperCase() : '';
});
},
/**
Returns the UpperCamelCase form of a string.
'innerHTML'.classify() => 'InnerHTML'
'action_name'.classify() => 'ActionName'
'css-class-name'.classify() => 'CssClassName'
'my favorite items'.classify() => 'MyFavoriteItems'
@method classify
@param {String} str the string to classify
@return {String} the classified string
*/
classify: function(str) {
var camelized = Ember.String.camelize(str);
return camelized.charAt(0).toUpperCase() + camelized.substr(1);
},
/**
More general than decamelize. Returns the lower_case_and_underscored
form of a string.
'innerHTML'.underscore() => 'inner_html'
'action_name'.underscore() => 'action_name'
'css-class-name'.underscore() => 'css_class_name'
'my favorite items'.underscore() => 'my_favorite_items'
@property underscore
@param {String} str The string to underscore.
@return {String} the underscored string.
*/
underscore: function(str) {
return str.replace(STRING_UNDERSCORE_REGEXP_1, '$1_$2').
replace(STRING_UNDERSCORE_REGEXP_2, '_').toLowerCase();
}
};
})();
(function() {
/**
@module ember
@submodule ember-runtime
*/
var fmt = Ember.String.fmt,
w = Ember.String.w,
loc = Ember.String.loc,
camelize = Ember.String.camelize,
decamelize = Ember.String.decamelize,
dasherize = Ember.String.dasherize,
underscore = Ember.String.underscore,
classify = Ember.String.classify;
if (Ember.EXTEND_PROTOTYPES === true || Ember.EXTEND_PROTOTYPES.String) {
/**
See {{#crossLink "Ember.String/fmt"}}{{/crossLink}}
@method fmt
@for String
*/
String.prototype.fmt = function() {
return fmt(this, arguments);
};
/**
See {{#crossLink "Ember.String/w"}}{{/crossLink}}
@method w
@for String
*/
String.prototype.w = function() {
return w(this);
};
/**
See {{#crossLink "Ember.String/loc"}}{{/crossLink}}
@method loc
@for String
*/
String.prototype.loc = function() {
return loc(this, arguments);
};
/**
See {{#crossLink "Ember.String/camelize"}}{{/crossLink}}
@method camelize
@for String
*/
String.prototype.camelize = function() {
return camelize(this);
};
/**
See {{#crossLink "Ember.String/decamelize"}}{{/crossLink}}
@method decamelize
@for String
*/
String.prototype.decamelize = function() {
return decamelize(this);
};
/**
See {{#crossLink "Ember.String/dasherize"}}{{/crossLink}}
@method dasherize
@for String
*/
String.prototype.dasherize = function() {
return dasherize(this);
};
/**
See {{#crossLink "Ember.String/underscore"}}{{/crossLink}}
@method underscore
@for String
*/
String.prototype.underscore = function() {
return underscore(this);
};
/**
See {{#crossLink "Ember.String/classify"}}{{/crossLink}}
@method classify
@for String
*/
String.prototype.classify = function() {
return classify(this);
};
}
})();
(function() {
/**
@module ember
@submodule ember-runtime
*/
var a_slice = Array.prototype.slice;
if (Ember.EXTEND_PROTOTYPES === true || Ember.EXTEND_PROTOTYPES.Function) {
/**
The `property` extension of Javascript's Function prototype is available
when Ember.EXTEND_PROTOTYPES or Ember.EXTEND_PROTOTYPES.Function is true,
which is the default.
Computed properties allow you to treat a function like a property:
MyApp.president = Ember.Object.create({
firstName: "Barack",
lastName: "Obama",
fullName: function() {
return this.get('firstName') + ' ' + this.get('lastName');
// Call this flag to mark the function as a property
}.property()
});
MyApp.president.get('fullName'); => "Barack Obama"
Treating a function like a property is useful because they can work with
bindings, just like any other property.
Many computed properties have dependencies on other properties. For
example, in the above example, the `fullName` property depends on
`firstName` and `lastName` to determine its value. You can tell Ember.js
about these dependencies like this:
MyApp.president = Ember.Object.create({
firstName: "Barack",
lastName: "Obama",
fullName: function() {
return this.get('firstName') + ' ' + this.get('lastName');
// Tell Ember.js that this computed property depends on firstName
// and lastName
}.property('firstName', 'lastName')
});
Make sure you list these dependencies so Ember.js knows when to update
bindings that connect to a computed property. Changing a dependency
will not immediately trigger an update of the computed property, but
will instead clear the cache so that it is updated when the next `get`
is called on the property.
See {{#crossLink "Ember.ComputedProperty"}}{{/crossLink}},
{{#crossLink "Ember/computed"}}{{/crossLink}}
@method property
@for Function
*/
Function.prototype.property = function() {
var ret = Ember.computed(this);
return ret.property.apply(ret, arguments);
};
/**
The `observes` extension of Javascript's Function prototype is available
when Ember.EXTEND_PROTOTYPES or Ember.EXTEND_PROTOTYPES.Function is true,
which is the default.
You can observe property changes simply by adding the `observes`
call to the end of your method declarations in classes that you write.
For example:
Ember.Object.create({
valueObserver: function() {
// Executes whenever the "value" property changes
}.observes('value')
});
See {{#crossLink "Ember.Observable/observes"}}{{/crossLink}}
@method observes
@for Function
*/
Function.prototype.observes = function() {
this.__ember_observes__ = a_slice.call(arguments);
return this;
};
/**
The `observesBefore` extension of Javascript's Function prototype is available
when Ember.EXTEND_PROTOTYPES or Ember.EXTEND_PROTOTYPES.Function is true,
which is the default.
You can get notified when a property changes is about to happen by
by adding the `observesBefore` call to the end of your method
declarations in classes that you write. For example:
Ember.Object.create({
valueObserver: function() {
// Executes whenever the "value" property is about to change
}.observesBefore('value')
});
See {{#crossLink "Ember.Observable/observesBefore"}}{{/crossLink}}
@method observesBefore
@for Function
*/
Function.prototype.observesBefore = function() {
this.__ember_observesBefore__ = a_slice.call(arguments);
return this;
};
}
})();
(function() {
})();
(function() {
/**
@module ember
@submodule ember-runtime
*/
// ..........................................................
// HELPERS
//
var get = Ember.get, set = Ember.set;
var a_slice = Array.prototype.slice;
var a_indexOf = Ember.EnumerableUtils.indexOf;
var contexts = [];
function popCtx() {
return contexts.length===0 ? {} : contexts.pop();
}
function pushCtx(ctx) {
contexts.push(ctx);
return null;
}
function iter(key, value) {
var valueProvided = arguments.length === 2;
function i(item) {
var cur = get(item, key);
return valueProvided ? value===cur : !!cur;
}
return i ;
}
/**
This mixin defines the common interface implemented by enumerable objects
in Ember. Most of these methods follow the standard Array iteration
API defined up to JavaScript 1.8 (excluding language-specific features that
cannot be emulated in older versions of JavaScript).
This mixin is applied automatically to the Array class on page load, so you
can use any of these methods on simple arrays. If Array already implements
one of these methods, the mixin will not override them.
h3. Writing Your Own Enumerable
To make your own custom class enumerable, you need two items:
1. You must have a length property. This property should change whenever
the number of items in your enumerable object changes. If you using this
with an Ember.Object subclass, you should be sure to change the length
property using set().
2. If you must implement nextObject(). See documentation.
Once you have these two methods implement, apply the Ember.Enumerable mixin
to your class and you will be able to enumerate the contents of your object
like any other collection.
h3. Using Ember Enumeration with Other Libraries
Many other libraries provide some kind of iterator or enumeration like
facility. This is often where the most common API conflicts occur.
Ember's API is designed to be as friendly as possible with other
libraries by implementing only methods that mostly correspond to the
JavaScript 1.8 API.
@class Enumerable
@namespace Ember
@extends Ember.Mixin
@since Ember 0.9
*/
Ember.Enumerable = Ember.Mixin.create(
/** @scope Ember.Enumerable.prototype */ {
// compatibility
isEnumerable: true,
/**
Implement this method to make your class enumerable.
This method will be call repeatedly during enumeration. The index value
will always begin with 0 and increment monotonically. You don't have to
rely on the index value to determine what object to return, but you should
always check the value and start from the beginning when you see the
requested index is 0.
The previousObject is the object that was returned from the last call
to nextObject for the current iteration. This is a useful way to
manage iteration if you are tracing a linked list, for example.
Finally the context parameter will always contain a hash you can use as
a "scratchpad" to maintain any other state you need in order to iterate
properly. The context object is reused and is not reset between
iterations so make sure you setup the context with a fresh state whenever
the index parameter is 0.
Generally iterators will continue to call nextObject until the index
reaches the your current length-1. If you run out of data before this
time for some reason, you should simply return undefined.
The default implementation of this method simply looks up the index.
This works great on any Array-like objects.
@method nextObject
@param {Number} index the current index of the iteration
@param {Object} previousObject the value returned by the last call to nextObject.
@param {Object} context a context object you can use to maintain state.
@return {Object} the next object in the iteration or undefined
*/
nextObject: Ember.required(Function),
/**
Helper method returns the first object from a collection. This is usually
used by bindings and other parts of the framework to extract a single
object if the enumerable contains only one item.
If you override this method, you should implement it so that it will
always return the same value each time it is called. If your enumerable
contains only one object, this method should always return that object.
If your enumerable is empty, this method should return undefined.
var arr = ["a", "b", "c"];
arr.firstObject(); => "a"
var arr = [];
arr.firstObject(); => undefined
@property firstObject
@return {Object} the object or undefined
*/
firstObject: Ember.computed(function() {
if (get(this, 'length')===0) return undefined ;
// handle generic enumerables
var context = popCtx(), ret;
ret = this.nextObject(0, null, context);
pushCtx(context);
return ret ;
}).property('[]'),
/**
Helper method returns the last object from a collection. If your enumerable
contains only one object, this method should always return that object.
If your enumerable is empty, this method should return undefined.