-
Notifications
You must be signed in to change notification settings - Fork 6
/
data.js
5625 lines (4330 loc) · 157 KB
/
data.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("./runtime");
require("./states");
(function(window) {
(function() {
window.DS = Ember.Namespace.create({
CURRENT_API_REVISION: 6
});
})();
(function() {
var get = Ember.get, set = Ember.set;
/**
A record array is an array that contains records of a certain type. The record
array materializes records as needed when they are retrieved for the first
time. You should not create record arrays yourself. Instead, an instance of
DS.RecordArray or its subclasses will be returned by your application's store
in response to queries.
*/
DS.RecordArray = Ember.ArrayProxy.extend(Ember.Evented, {
/**
The model type contained by this record array.
@type DS.Model
*/
type: null,
// The array of client ids backing the record array. When a
// record is requested from the record array, the record
// for the client id at the same index is materialized, if
// necessary, by the store.
content: null,
isLoaded: false,
isUpdating: false,
// The store that created this record array.
store: null,
objectAtContent: function(index) {
var content = get(this, 'content'),
clientId = content.objectAt(index),
store = get(this, 'store');
if (clientId !== undefined) {
return store.findByClientId(get(this, 'type'), clientId);
}
},
materializedObjectAt: function(index) {
var clientId = get(this, 'content').objectAt(index);
if (!clientId) { return; }
if (get(this, 'store').recordIsMaterialized(clientId)) {
return this.objectAt(index);
}
},
update: function() {
if (get(this, 'isUpdating')) { return; }
var store = get(this, 'store'),
type = get(this, 'type');
store.fetchAll(type, this);
}
});
})();
(function() {
var get = Ember.get;
DS.FilteredRecordArray = DS.RecordArray.extend({
filterFunction: null,
isLoaded: true,
replace: function() {
var type = get(this, 'type').toString();
throw new Error("The result of a client-side filter (on " + type + ") is immutable.");
},
updateFilter: Ember.observer(function() {
var store = get(this, 'store');
store.updateRecordArrayFilter(this, get(this, 'type'), get(this, 'filterFunction'));
}, 'filterFunction')
});
})();
(function() {
var get = Ember.get, set = Ember.set;
DS.AdapterPopulatedRecordArray = DS.RecordArray.extend({
query: null,
replace: function() {
var type = get(this, 'type').toString();
throw new Error("The result of a server query (on " + type + ") is immutable.");
},
load: function(array) {
var store = get(this, 'store'), type = get(this, 'type');
var clientIds = store.loadMany(type, array).clientIds;
this.beginPropertyChanges();
set(this, 'content', Ember.A(clientIds));
set(this, 'isLoaded', true);
this.endPropertyChanges();
this.trigger('didLoad');
}
});
})();
(function() {
var get = Ember.get, set = Ember.set, guidFor = Ember.guidFor;
var Set = function() {
this.hash = {};
this.list = [];
};
Set.prototype = {
add: function(item) {
var hash = this.hash,
guid = guidFor(item);
if (hash.hasOwnProperty(guid)) { return; }
hash[guid] = true;
this.list.push(item);
},
remove: function(item) {
var hash = this.hash,
guid = guidFor(item);
if (!hash.hasOwnProperty(guid)) { return; }
delete hash[guid];
var list = this.list,
index = Ember.EnumerableUtils.indexOf(this, item);
list.splice(index, 1);
},
isEmpty: function() {
return this.list.length === 0;
}
};
var LoadedState = Ember.State.extend({
recordWasAdded: function(manager, record) {
var dirty = manager.dirty, observer;
dirty.add(record);
observer = function() {
if (!get(record, 'isDirty')) {
record.removeObserver('isDirty', observer);
manager.send('childWasSaved', record);
}
};
record.addObserver('isDirty', observer);
},
recordWasRemoved: function(manager, record) {
var dirty = manager.dirty, observer;
dirty.add(record);
observer = function() {
record.removeObserver('isDirty', observer);
if (!get(record, 'isDirty')) { manager.send('childWasSaved', record); }
};
record.addObserver('isDirty', observer);
}
});
var states = {
loading: Ember.State.create({
isLoaded: false,
isDirty: false,
loadedRecords: function(manager, count) {
manager.decrement(count);
},
becameLoaded: function(manager) {
manager.transitionTo('clean');
}
}),
clean: LoadedState.create({
isLoaded: true,
isDirty: false,
recordWasAdded: function(manager, record) {
this._super(manager, record);
manager.goToState('dirty');
},
update: function(manager, clientIds) {
var manyArray = manager.manyArray;
set(manyArray, 'content', clientIds);
}
}),
dirty: LoadedState.create({
isLoaded: true,
isDirty: true,
childWasSaved: function(manager, child) {
var dirty = manager.dirty;
dirty.remove(child);
if (dirty.isEmpty()) { manager.send('arrayBecameSaved'); }
},
arrayBecameSaved: function(manager) {
manager.goToState('clean');
}
})
};
DS.ManyArrayStateManager = Ember.StateManager.extend({
manyArray: null,
initialState: 'loading',
states: states,
/**
This number is used to keep track of the number of outstanding
records that must be loaded before the array is considered
loaded. As results stream in, this number is decremented until
it becomes zero, at which case the `isLoaded` flag will be set
to true
*/
counter: 0,
init: function() {
this._super();
this.dirty = new Set();
this.counter = get(this, 'manyArray.length');
},
decrement: function(count) {
var counter = this.counter = this.counter - count;
Ember.assert("Somehow the ManyArray loaded counter went below 0. This is probably an ember-data bug. Please report it at https://github.com/emberjs/data/issues", counter >= 0);
if (counter === 0) {
this.send('becameLoaded');
}
}
});
})();
(function() {
var get = Ember.get, set = Ember.set;
/**
A ManyArray is a RecordArray that represents the contents of a has-many
association.
The ManyArray is instantiated lazily the first time the association is
requested.
### Inverses
Often, the associations in Ember Data applications will have
an inverse. For example, imagine the following models are
defined:
App.Post = DS.Model.extend({
comments: DS.hasMany('App.Comment')
});
App.Comment = DS.Model.extend({
post: DS.belongsTo('App.Post')
});
If you created a new instance of `App.Post` and added
a `App.Comment` record to its `comments` has-many
association, you would expect the comment's `post`
property to be set to the post that contained
the has-many.
We call the record to which an association belongs the
association's _owner_.
*/
DS.ManyArray = DS.RecordArray.extend({
init: function() {
this._super.apply(this, arguments);
this._changesToSync = Ember.OrderedSet.create();
},
/**
@private
The record to which this association belongs.
@property {DS.Model}
*/
owner: null,
// LOADING STATE
isLoaded: false,
loadingRecordsCount: function(count) {
this.loadingRecordsCount = count;
},
loadedRecord: function() {
this.loadingRecordsCount--;
if (this.loadingRecordsCount === 0) {
set(this, 'isLoaded', true);
this.trigger('didLoad');
}
},
fetch: function() {
var clientIds = get(this, 'content'),
store = get(this, 'store'),
type = get(this, 'type');
store.fetchUnloadedClientIds(type, clientIds);
},
// Overrides Ember.Array's replace method to implement
replaceContent: function(index, removed, added) {
// Map the array of record objects into an array of client ids.
added = added.map(function(record) {
Ember.assert("You can only add records of " + (get(this, 'type') && get(this, 'type').toString()) + " to this association.", !get(this, 'type') || (get(this, 'type') === record.constructor));
return record.get('clientId');
}, this);
this._super(index, removed, added);
},
arrangedContentDidChange: function() {
this.fetch();
},
arrayContentWillChange: function(index, removed, added) {
var owner = get(this, 'owner'),
name = get(this, 'name');
if (!owner._suspendedAssociations) {
// This code is the first half of code that continues inside
// of arrayContentDidChange. It gets or creates a change from
// the child object, adds the current owner as the old
// parent if this is the first time the object was removed
// from a ManyArray, and sets `newParent` to null.
//
// Later, if the object is added to another ManyArray,
// the `arrayContentDidChange` will set `newParent` on
// the change.
for (var i=index; i<index+removed; i++) {
var clientId = get(this, 'content').objectAt(i);
//var record = this.objectAt(i);
//if (!record) { continue; }
var change = DS.OneToManyChange.forChildAndParent(clientId, get(this, 'store'), { parentType: owner.constructor });
change.hasManyName = name;
if (change.oldParent === undefined) { change.oldParent = get(owner, 'clientId'); }
change.newParent = null;
this._changesToSync.add(change);
}
}
return this._super.apply(this, arguments);
},
arrayContentDidChange: function(index, removed, added) {
this._super.apply(this, arguments);
var owner = get(this, 'owner'),
name = get(this, 'name');
if (!owner._suspendedAssociations) {
// This code is the second half of code that started in
// `arrayContentWillChange`. It gets or creates a change
// from the child object, and adds the current owner as
// the new parent.
for (var i=index; i<index+added; i++) {
var clientId = get(this, 'content').objectAt(i);
var change = DS.OneToManyChange.forChildAndParent(clientId, get(this, 'store'), { parentType: owner.constructor });
change.hasManyName = name;
// The oldParent will be looked up in `sync` if it
// was not set by `belongsToWillChange`.
change.newParent = get(owner, 'clientId');
this._changesToSync.add(change);
}
// We wait until the array has finished being
// mutated before syncing the OneToManyChanges created
// in arrayContentWillChange, so that the array
// membership test in the sync() logic operates
// on the final results.
this._changesToSync.forEach(function(change) {
change.sync();
});
this._changesToSync.clear();
}
},
/**
@private
*/
assignInverse: function(record) {
var inverseName = DS.inverseNameFor(record.constructor, get(this, 'owner.constructor'), 'belongsTo'),
owner = get(this, 'owner'),
currentInverse;
if (inverseName) {
currentInverse = get(record, inverseName);
if (currentInverse !== owner) {
set(record, inverseName, owner);
}
}
return currentInverse;
},
/**
@private
*/
removeInverse: function(record) {
var inverseName = DS.inverseNameFor(record.constructor, get(this, 'owner.constructor'), 'belongsTo');
if (inverseName) {
var currentInverse = get(record, inverseName);
if (currentInverse === get(this, 'owner')) {
set(record, inverseName, null);
}
}
},
// Create a child record within the owner
createRecord: function(hash, transaction) {
var owner = get(this, 'owner'),
store = get(owner, 'store'),
type = get(this, 'type'),
record;
transaction = transaction || get(owner, 'transaction');
record = store.createRecord.call(store, type, hash, transaction);
this.pushObject(record);
return record;
},
/**
METHODS FOR USE BY INVERSE RELATIONSHIPS
========================================
These methods exists so that belongsTo relationships can
set their inverses without causing an infinite loop.
This creates two APIs:
* the normal enumerable API, which is used by clients
of the `ManyArray` and triggers a change to inverse
`belongsTo` relationships.
* `removeFromContent` and `addToContent`, which are
used by inverse relationships and do not trigger a
change to `belongsTo` relationships.
Unlike the normal `addObject` and `removeObject` APIs,
these APIs manipulate the `content` array without
triggering side-effects.
*/
/** @private */
removeFromContent: function(record) {
var clientId = get(record, 'clientId');
get(this, 'content').removeObject(clientId);
},
/** @private */
addToContent: function(record) {
var clientId = get(record, 'clientId');
get(this, 'content').addObject(clientId);
}
});
})();
(function() {
})();
(function() {
var get = Ember.get, set = Ember.set, fmt = Ember.String.fmt,
removeObject = Ember.EnumerableUtils.removeObject, forEach = Ember.EnumerableUtils.forEach;
var RelationshipLink = function(parent, child) {
this.oldParent = parent;
this.child = child;
};
/**
A transaction allows you to collect multiple records into a unit of work
that can be committed or rolled back as a group.
For example, if a record has local modifications that have not yet
been saved, calling `commit()` on its transaction will cause those
modifications to be sent to the adapter to be saved. Calling
`rollback()` on its transaction would cause all of the modifications to
be discarded and the record to return to the last known state before
changes were made.
If a newly created record's transaction is rolled back, it will
immediately transition to the deleted state.
If you do not explicitly create a transaction, a record is assigned to
an implicit transaction called the default transaction. In these cases,
you can treat your application's instance of `DS.Store` as a transaction
and call the `commit()` and `rollback()` methods on the store itself.
Once a record has been successfully committed or rolled back, it will
be moved back to the implicit transaction. Because it will now be in
a clean state, it can be moved to a new transaction if you wish.
### Creating a Transaction
To create a new transaction, call the `transaction()` method of your
application's `DS.Store` instance:
var transaction = App.store.transaction();
This will return a new instance of `DS.Transaction` with no records
yet assigned to it.
### Adding Existing Records
Add records to a transaction using the `add()` method:
record = App.store.find(App.Person, 1);
transaction.add(record);
Note that only records whose `isDirty` flag is `false` may be added
to a transaction. Once modifications to a record have been made
(its `isDirty` flag is `true`), it is not longer able to be added to
a transaction.
### Creating New Records
Because newly created records are dirty from the time they are created,
and because dirty records can not be added to a transaction, you must
use the `createRecord()` method to assign new records to a transaction.
For example, instead of this:
var transaction = store.transaction();
var person = App.Person.createRecord({ name: "Steve" });
// won't work because person is dirty
transaction.add(person);
Call `createRecord()` on the transaction directly:
var transaction = store.transaction();
transaction.createRecord(App.Person, { name: "Steve" });
### Asynchronous Commits
Typically, all of the records in a transaction will be committed
together. However, new records that have a dependency on other new
records need to wait for their parent record to be saved and assigned an
ID. In that case, the child record will continue to live in the
transaction until its parent is saved, at which time the transaction will
attempt to commit again.
For this reason, you should not re-use transactions once you have committed
them. Always make a new transaction and move the desired records to it before
calling commit.
*/
var arrayDefault = function() { return []; };
DS.Transaction = Ember.Object.extend({
/**
@private
Creates the bucket data structure used to segregate records by
type.
*/
init: function() {
set(this, 'buckets', {
clean: Ember.OrderedSet.create(),
created: Ember.OrderedSet.create(),
updated: Ember.OrderedSet.create(),
deleted: Ember.OrderedSet.create(),
inflight: Ember.OrderedSet.create()
});
set(this, 'relationships', Ember.OrderedSet.create());
},
/**
Creates a new record of the given type and assigns it to the transaction
on which the method was called.
This is useful as only clean records can be added to a transaction and
new records created using other methods immediately become dirty.
@param {DS.Model} type the model type to create
@param {Object} hash the data hash to assign the new record
*/
createRecord: function(type, hash) {
var store = get(this, 'store');
return store.createRecord(type, hash, this);
},
isEqualOrDefault: function(other) {
if (this === other || other === get(this, 'store.defaultTransaction')) {
return true;
}
},
isDefault: Ember.computed(function() {
return this === get(this, 'store.defaultTransaction');
}),
/**
Adds an existing record to this transaction. Only records without
modficiations (i.e., records whose `isDirty` property is `false`)
can be added to a transaction.
@param {DS.Model} record the record to add to the transaction
*/
add: function(record) {
Ember.assert("You must pass a record into transaction.add()", record instanceof DS.Model);
var recordTransaction = get(record, 'transaction'),
defaultTransaction = get(this, 'store.defaultTransaction');
// Make `add` idempotent
if (recordTransaction === this) { return; }
// XXX it should be possible to move a dirty transaction from the default transaction
// we could probably make this work if someone has a valid use case. Do you?
Ember.assert("Once a record has changed, you cannot move it into a different transaction", !get(record, 'isDirty'));
Ember.assert("Models cannot belong to more than one transaction at a time.", recordTransaction === defaultTransaction);
this.adoptRecord(record);
},
relationshipBecameDirty: function(relationship) {
get(this, 'relationships').add(relationship);
},
relationshipBecameClean: function(relationship) {
get(this, 'relationships').remove(relationship);
},
/**
Commits the transaction, which causes all of the modified records that
belong to the transaction to be sent to the adapter to be saved.
Once you call `commit()` on a transaction, you should not re-use it.
When a record is saved, it will be removed from this transaction and
moved back to the store's default transaction.
*/
commit: function() {
var store = get(this, 'store');
var adapter = get(store, '_adapter');
var defaultTransaction = get(store, 'defaultTransaction');
var iterate = function(records) {
var set = records.copy();
set.forEach(function (record) {
record.send('willCommit');
});
return set;
};
var relationships = get(this, 'relationships');
var commitDetails = {
created: iterate(this.bucketForType('created')),
updated: iterate(this.bucketForType('updated')),
deleted: iterate(this.bucketForType('deleted')),
relationships: relationships
};
if (this === defaultTransaction) {
set(store, 'defaultTransaction', store.transaction());
}
this.removeCleanRecords();
if (!commitDetails.created.isEmpty() || !commitDetails.updated.isEmpty() || !commitDetails.deleted.isEmpty() || !relationships.isEmpty()) {
if (adapter && adapter.commit) { adapter.commit(store, commitDetails); }
else { throw fmt("Adapter is either null or does not implement `commit` method", this); }
}
},
/**
Rolling back a transaction resets the records that belong to
that transaction.
Updated records have their properties reset to the last known
value from the persistence layer. Deleted records are reverted
to a clean, non-deleted state. Newly created records immediately
become deleted, and are not sent to the adapter to be persisted.
After the transaction is rolled back, any records that belong
to it will return to the store's default transaction, and the
current transaction should not be used again.
*/
rollback: function() {
// Loop through all of the records in each of the dirty states
// and initiate a rollback on them. As a side effect of telling
// the record to roll back, it should also move itself out of
// the dirty bucket and into the clean bucket.
['created', 'updated', 'deleted', 'inflight'].forEach(function(bucketType) {
var records = this.bucketForType(bucketType);
forEach(records, function(record) {
record.send('rollback');
});
records.clear();
}, this);
// Now that all records in the transaction are guaranteed to be
// clean, migrate them all to the store's default transaction.
this.removeCleanRecords();
},
/**
@private
Removes a record from this transaction and back to the store's
default transaction.
Note: This method is private for now, but should probably be exposed
in the future once we have stricter error checking (for example, in the
case of the record being dirty).
@param {DS.Model} record
*/
remove: function(record) {
var defaultTransaction = get(this, 'store.defaultTransaction');
defaultTransaction.adoptRecord(record);
},
/**
@private
Removes all of the records in the transaction's clean bucket.
*/
removeCleanRecords: function() {
var clean = this.bucketForType('clean');
clean.forEach(function(record) {
this.remove(record);
}, this);
clean.clear();
},
/**
@private
Returns the bucket for the given bucket type. For example, you might call
`this.bucketForType('updated')` to get the `Ember.Map` that contains all
of the records that have changes pending.
@param {String} bucketType the type of bucket
@returns Ember.Map
*/
bucketForType: function(bucketType) {
var buckets = get(this, 'buckets');
return get(buckets, bucketType);
},
/**
@private
This method moves a record into a different transaction without the normal
checks that ensure that the user is not doing something weird, like moving
a dirty record into a new transaction.
It is designed for internal use, such as when we are moving a clean record
into a new transaction when the transaction is committed.
This method must not be called unless the record is clean.
@param {DS.Model} record
*/
adoptRecord: function(record) {
var oldTransaction = get(record, 'transaction');
if (oldTransaction) {
oldTransaction.removeFromBucket('clean', record);
}
this.addToBucket('clean', record);
set(record, 'transaction', this);
},
/**
@private
Adds a record to the named bucket.
@param {String} bucketType one of `clean`, `created`, `updated`, or `deleted`
*/
addToBucket: function(bucketType, record) {
this.bucketForType(bucketType).add(record);
},
/**
@private
Removes a record from the named bucket.
@param {String} bucketType one of `clean`, `created`, `updated`, or `deleted`
*/
removeFromBucket: function(bucketType, record) {
this.bucketForType(bucketType).remove(record);
},
/**
@private
Called by a record's state manager to indicate that the record has entered
a dirty state. The record will be moved from the `clean` bucket and into
the appropriate dirty bucket.
@param {String} bucketType one of `created`, `updated`, or `deleted`
*/
recordBecameDirty: function(bucketType, record) {
this.removeFromBucket('clean', record);
this.addToBucket(bucketType, record);
},
/**
@private
Called by a record's state manager to indicate that the record has entered
inflight state. The record will be moved from its current dirty bucket and into
the `inflight` bucket.
@param {String} bucketType one of `created`, `updated`, or `deleted`
*/
recordBecameInFlight: function(kind, record) {
this.removeFromBucket(kind, record);
this.addToBucket('inflight', record);
},
recordIsMoving: function(kind, record) {
this.removeFromBucket(kind, record);
this.addToBucket('clean', record);
},
/**
@private
Called by a record's state manager to indicate that the record has entered
a clean state. The record will be moved from its current dirty or inflight bucket and into
the `clean` bucket.
@param {String} bucketType one of `created`, `updated`, or `deleted`
*/
recordBecameClean: function(kind, record) {
this.removeFromBucket(kind, record);
this.remove(record);
}
});
})();
(function() {
/*globals Ember*/
var get = Ember.get, set = Ember.set, fmt = Ember.String.fmt;
// These values are used in the data cache when clientIds are
// needed but the underlying data has not yet been loaded by
// the server.
var UNLOADED = 'unloaded';
var LOADING = 'loading';
var MATERIALIZED = { materialized: true };
var CREATED = { created: true };
// Implementors Note:
//
// The variables in this file are consistently named according to the following
// scheme:
//
// * +id+ means an identifier managed by an external source, provided inside
// the data hash provided by that source.
// * +clientId+ means a transient numerical identifier generated at runtime by
// the data store. It is important primarily because newly created objects may
// not yet have an externally generated id.
// * +type+ means a subclass of DS.Model.
// Used by the store to normalize IDs entering the store. Despite the fact
// that developers may provide IDs as numbers (e.g., `store.find(Person, 1)`),
// it is important that internally we use strings, since IDs may be serialized
// and lose type information. For example, Ember's router may put a record's
// ID into the URL, and if we later try to deserialize that URL and find the
// corresponding record, we will not know if it is a string or a number.
var coerceId = function(id) {
return id+'';
};
var map = Ember.EnumerableUtils.map;
/**
The store contains all of the hashes for records loaded from the server.
It is also responsible for creating instances of DS.Model when you request one
of these data hashes, so that they can be bound to in your Handlebars templates.
Create a new store like this:
MyApp.store = DS.Store.create();
You can retrieve DS.Model instances from the store in several ways. To retrieve
a record for a specific id, use the `find()` method:
var record = MyApp.store.find(MyApp.Contact, 123);
By default, the store will talk to your backend using a standard REST mechanism.
You can customize how the store talks to your backend by specifying a custom adapter:
MyApp.store = DS.Store.create({
adapter: 'MyApp.CustomAdapter'
});
You can learn more about writing a custom adapter by reading the `DS.Adapter`
documentation.
*/
DS.Store = Ember.Object.extend({
/**
Many methods can be invoked without specifying which store should be used.
In those cases, the first store created will be used as the default. If
an application has multiple stores, it should specify which store to use
when performing actions, such as finding records by id.
The init method registers this store as the default if none is specified.
*/
init: function() {
// Enforce API revisioning. See BREAKING_CHANGES.md for more.
var revision = get(this, 'revision');
if (revision !== DS.CURRENT_API_REVISION && !Ember.ENV.TESTING) {
throw new Error("Error: The Ember Data library has had breaking API changes since the last time you updated the library. Please review the list of breaking changes at https://github.com/emberjs/data/blob/master/BREAKING_CHANGES.md, then update your store's `revision` property to " + DS.CURRENT_API_REVISION);
}
if (!get(DS, 'defaultStore') || get(this, 'isDefaultStore')) {
set(DS, 'defaultStore', this);
}
// internal bookkeeping; not observable
this.typeMaps = {};
this.recordCache = [];
this.clientIdToId = {};
this.clientIdToType = {};
this.clientIdToHash = {};