(function() { window.DS = Ember.Namespace.create({ CURRENT_API_REVISION: 4 }); })(); (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({ /** 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, // The store that created this record array. store: null, init: function() { set(this, 'recordCache', Ember.A([])); this._super(); }, arrayDidChange: function(array, index, removed, added) { var recordCache = get(this, 'recordCache'); recordCache.replace(index, 0, new Array(added)); this._super(array, index, removed, added); }, arrayWillChange: function(array, index, removed, added) { this._super(array, index, removed, added); var recordCache = get(this, 'recordCache'); recordCache.replace(index, removed); }, objectAtContent: function(index) { var recordCache = get(this, 'recordCache'); var record = recordCache.objectAt(index); if (!record) { var store = get(this, 'store'); var content = get(this, 'content'); var contentObject = content.objectAt(index); if (contentObject !== undefined) { record = store.findByClientId(get(this, 'type'), contentObject); recordCache.replace(index, 1, [record]); } } return record; } }); })(); (function() { var get = Ember.get; DS.FilteredRecordArray = DS.RecordArray.extend({ filterFunction: null, 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, isLoaded: false, 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(); } }); })(); (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.ArrayUtils.indexOf(this, item); list.splice(index, 1); }, isEmpty: function() { return this.list.length === 0; } }; var ManyArrayState = 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 = { clean: ManyArrayState.create({ 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: ManyArrayState.create({ 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: 'clean', states: states, init: function() { this._super(); this.dirty = new Set(); } }); })(); (function() { var get = Ember.get, set = Ember.set, getPath = Ember.getPath; DS.ManyArray = DS.RecordArray.extend({ init: function() { set(this, 'stateManager', DS.ManyArrayStateManager.create({ manyArray: this })); return this._super(); }, parentRecord: null, isDirty: Ember.computed(function() { return getPath(this, 'stateManager.currentState.isDirty'); }).property('stateManager.currentState').cacheable(), fetch: function() { var clientIds = get(this, 'content'), store = get(this, 'store'), type = get(this, 'type'); var ids = clientIds.map(function(clientId) { return store.clientIdToId[clientId]; }); store.fetchMany(type, ids); }, // Overrides Ember.Array's replace method to implement replace: function(index, removed, added) { var parentRecord = get(this, 'parentRecord'); var pendingParent = parentRecord && !get(parentRecord, 'id'); var stateManager = get(this, 'stateManager'); // Map the array of record objects into an array of client ids. added = added.map(function(record) { // If the record to which this many array belongs does not yet // have an id, notify the newly-added record that it must wait // for the parent to receive an id before the child can be // saved. if (pendingParent) { record.send('waitingOn', parentRecord); } this.assignInverse(record, parentRecord); stateManager.send('recordWasAdded', record); return record.get('clientId'); }, this); var store = this.store; var len = index+removed, record; for (var i = index; i < len; i++) { // TODO: null out inverse FK record = this.objectAt(i); this.assignInverse(record, parentRecord, true); // If we put the child record into a pending state because // we were waiting on the parent record to get an id, we // can tell the child it no longer needs to wait. if (pendingParent) { record.send('doneWaitingOn', parentRecord); } stateManager.send('recordWasAdded', record); } this._super(index, removed, added); }, assignInverse: function(record, parentRecord, remove) { var associationMap = get(record.constructor, 'associations'), possibleAssociations = associationMap.get(parentRecord.constructor), possible, actual; if (!possibleAssociations) { return; } for (var i = 0, l = possibleAssociations.length; i < l; i++) { possible = possibleAssociations[i]; if (possible.kind === 'belongsTo') { actual = possible; break; } } if (actual) { set(record, actual.name, remove ? null : parentRecord); } }, // Create a child record within the parentRecord createRecord: function(hash, transaction) { var parentRecord = get(this, 'parentRecord'), store = get(parentRecord, 'store'), type = get(this, 'type'), record; transaction = transaction || get(parentRecord, 'transaction'); record = store.createRecord.call(store, type, hash, transaction); this.pushObject(record); return record; } }); })(); (function() { })(); (function() { var get = Ember.get, set = Ember.set, getPath = Ember.getPath, fmt = Ember.String.fmt; /** 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(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 = 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(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. */ DS.Transaction = Ember.Object.extend({ /** @private Creates the bucket data structure used to segregate records by type. */ init: function() { set(this, 'buckets', { clean: Ember.Map.create(), created: Ember.Map.create(), updated: Ember.Map.create(), deleted: Ember.Map.create(), inflight: Ember.Map.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); }, /** 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) { // we could probably make this work if someone has a valid use case. Do you? var recordTransaction = get(record, 'transaction'), defaultTransaction = getPath(this, 'store.defaultTransaction'); this.adoptRecord(record); }, /** 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 self = this, iterate; iterate = function(bucketType, fn, binding) { var dirty = self.bucketForType(bucketType); dirty.forEach(function(type, records) { if (records.isEmpty()) { return; } var array = []; records.forEach(function(record) { record.send('willCommit'); if (get(record, 'isPending') === false) { array.push(record); } }); fn.call(binding, type, array); }); }; var commitDetails = { updated: { eachType: function(fn, binding) { iterate('updated', fn, binding); } }, created: { eachType: function(fn, binding) { iterate('created', fn, binding); } }, deleted: { eachType: function(fn, binding) { iterate('deleted', fn, binding); } } }; var store = get(this, 'store'); var adapter = get(store, '_adapter'); this.removeCleanRecords(); 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() { var store = get(this, 'store'), dirty; // 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) { dirty = this.bucketForType(bucketType); dirty.forEach(function(type, records) { records.forEach(function(record) { record.send('rollback'); }); }); }, 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 = getPath(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'), self = this; clean.forEach(function(type, records) { records.forEach(function(record) { self.remove(record); }); }); }, /** @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) { var bucket = this.bucketForType(bucketType), type = record.constructor; var records = bucket.get(type); if (!records) { records = Ember.OrderedSet.create(); bucket.set(type, records); } records.add(record); }, /** @private Removes a record from the named bucket. @param {String} bucketType one of `clean`, `created`, `updated`, or `deleted` */ removeFromBucket: function(bucketType, record) { var bucket = this.bucketForType(bucketType), type = record.constructor; var records = bucket.get(type); records.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); }, /** @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, getPath = Ember.getPath, fmt = Ember.String.fmt; var DATA_PROXY = { get: function(name) { return this.savedData[name]; } }; // 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'; // 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. /** 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.recordArraysByClientId = {}; set(this, 'defaultTransaction', this.transaction()); return this._super(); }, /** Returns a new transaction scoped to this store. @see {DS.Transaction} @returns DS.Transaction */ transaction: function() { return DS.Transaction.create({ store: this }); }, /** @private This is used only by the record's DataProxy. Do not use this directly. */ dataForRecord: function(record) { var type = record.constructor, clientId = get(record, 'clientId'), typeMap = this.typeMapFor(type); return typeMap.cidToHash[clientId]; }, /** The adapter to use to communicate to a backend server or other persistence layer. This can be specified as an instance, a class, or a property path that specifies where the adapter can be located. @property {DS.Adapter|String} */ adapter: null, /** @private This property returns the adapter, after resolving a possible String. @returns DS.Adapter */ _adapter: Ember.computed(function() { var adapter = get(this, 'adapter'); if (typeof adapter === 'string') { return getPath(this, adapter, false) || getPath(window, adapter); } return adapter; }).property('adapter').cacheable(), // A monotonically increasing number to be used to uniquely identify // data hashes and records. clientIdCounter: 1, // ..................... // . CREATE NEW RECORD . // ..................... /** Create a new record in the current store. The properties passed to this method are set on the newly created record. @param {subclass of DS.Model} type @param {Object} properties a hash of properties to set on the newly created record. @returns DS.Model */ createRecord: function(type, properties, transaction) { properties = properties || {}; // Create a new instance of the model `type` and put it // into the specified `transaction`. If no transaction is // specified, the default transaction will be used. // // NOTE: A `transaction` is specified when the // `transaction.createRecord` API is used. var record = type._create({ store: this }); transaction = transaction || get(this, 'defaultTransaction'); transaction.adoptRecord(record); // Extract the primary key from the `properties` hash, // based on the `primaryKey` for the model type. var primaryKey = get(record, 'primaryKey'), id = properties[primaryKey] || null; // If the passed properties do not include a primary key, // give the adapter an opportunity to generate one. var adapter; if (Ember.none(id)) { adapter = get(this, 'adapter'); if (adapter && adapter.generateIdForRecord) { id = adapter.generateIdForRecord(this, record); properties.id = id; } } var hash = {}, clientId; // Push the hash into the store. If present, associate the // extracted `id` with the hash. clientId = this.pushHash(hash, id, type); record.send('didChangeData'); var recordCache = get(this, 'recordCache'); // Now that we have a clientId, attach it to the record we // just created. set(record, 'clientId', clientId); // Store the record we just created in the record cache for // this clientId. recordCache[clientId] = record; // Set the properties specified on the record. record.setProperties(properties); this.updateRecordArrays(type, clientId, get(record, 'data')); return record; }, // ................. // . DELETE RECORD . // ................. /** For symmetry, a record can be deleted via the store. @param {DS.Model} record */ deleteRecord: function(record) { record.send('deleteRecord'); }, // ................ // . FIND RECORDS . // ................ /** This is the main entry point into finding records. The first parameter to this method is always a subclass of `DS.Model`. You can use the `find` method on a subclass of `DS.Model` directly if your application only has one store. For example, instead of `store.find(App.Person, 1)`, you could say `App.Person.find(1)`. --- To find a record by ID, pass the `id` as the second parameter: store.find(App.Person, 1); App.Person.find(1); If the record with that `id` had not previously been loaded, the store will return an empty record immediately and ask the adapter to find the data by calling the adapter's `find` method. The `find` method will always return the same object for a given type and `id`. To check whether the adapter has populated a record, you can check its `isLoaded` property. --- To find all records for a type, call `find` with no additional parameters: store.find(App.Person); App.Person.find(); This will return a `RecordArray` representing all known records for the given type and kick off a request to the adapter's `findAll` method to load any additional records for the type. The `RecordArray` returned by `find()` is live. If any more records for the type are added at a later time through any mechanism, it will automatically update to reflect the change. --- To find a record by a query, call `find` with a hash as the second parameter: store.find(App.Person, { page: 1 }); App.Person.find({ page: 1 }); This will return a `RecordArray` immediately, but it will always be an empty `RecordArray` at first. It will call the adapter's `findQuery` method, which will populate the `RecordArray` once the server has returned results. You can check whether a query results `RecordArray` has loaded by checking its `isLoaded` property. */ find: function(type, id, query) { if (id === undefined) { return this.findAll(type); } if (query !== undefined) { return this.findMany(type, id, query); } else if (Ember.typeOf(id) === 'object') { return this.findQuery(type, id); } if (Ember.isArray(id)) { return this.findMany(type, id); } var clientId = this.typeMapFor(type).idToCid[id]; return this.findByClientId(type, clientId, id); }, findByClientId: function(type, clientId, id) { var recordCache = get(this, 'recordCache'), dataCache = this.typeMapFor(type).cidToHash, record; // If there is already a clientId assigned for this // type/id combination, try to find an existing // record for that id and return. Otherwise, // materialize a new record and set its data to the // value we already have. if (clientId !== undefined) { record = recordCache[clientId]; if (!record) { // create a new instance of the model type in the // 'isLoading' state record = this.materializeRecord(type, clientId); if (typeof dataCache[clientId] === 'object') { record.send('didChangeData'); } } } else { clientId = this.pushHash(LOADING, id, type); // create a new instance of the model type in the // 'isLoading' state record = this.materializeRecord(type, clientId); // let the adapter set the data, possibly async var adapter = get(this, '_adapter'); if (adapter && adapter.find) { adapter.find(this, type, id); } else { throw fmt("Adapter is either null or does not implement `find` method", this); } } return record; }, /** @private Ask the adapter to fetch IDs that are not already loaded. This method will convert `id`s to `clientId`s, filter out `clientId`s that already have a data hash present, and pass the remaining `id`s to the adapter. @param {Class} type A model class @param {Array} ids An array of ids @param {Object} query @returns {Array} An Array of all clientIds for the specified ids. */ fetchMany: function(type, ids, query) { var typeMap = this.typeMapFor(type), idToClientIdMap = typeMap.idToCid, dataCache = typeMap.cidToHash, data = typeMap.cidToHash, needed; var clientIds = Ember.A([]); if (ids) { needed = []; ids.forEach(function(id) { // Get the clientId for the given id var clientId = idToClientIdMap[id]; // If there is no `clientId` yet if (clientId === undefined) { // Create a new `clientId`, marking its data hash // as loading. Once the adapter returns the data // hash, it will be updated clientId = this.pushHash(LOADING, id, type); needed.push(id); // If there is a clientId, but its data hash is // marked as unloaded (this happens when a // hasMany association creates clientIds for its // referenced ids before they were loaded) } else if (clientId && data[clientId] === UNLOADED) { // change the data hash marker to loading dataCache[clientId] = LOADING; needed.push(id); } // this method is expected to return a list of // all of the clientIds for the specified ids, // unconditionally add it. clientIds.push(clientId); }, this); } else { needed = null; } // If there are any needed ids, ask the adapter to load them if ((needed && get(needed, 'length') > 0) || query) { var adapter = get(this, '_adapter'); if (adapter && adapter.findMany) { adapter.findMany(this, type, needed, query); } else { throw fmt("Adapter is either null or does not implement `findMany` method", this); } } return clientIds; }, /** @private */ findMany: function(type, ids, query) { var clientIds = this.fetchMany(type, ids, query); return this.createManyArray(type, clientIds); }, findQuery: function(type, query) { var array = DS.AdapterPopulatedRecordArray.create({ type: type, content: Ember.A([]), store: this }); var adapter = get(this, '_adapter'); if (adapter && adapter.findQuery) { adapter.findQuery(this, type, query, array); } else { throw fmt("Adapter is either null or does not implement `findQuery` method", this); } return array; }, findAll: function(type) { var typeMap = this.typeMapFor(type), findAllCache = typeMap.findAllCache; if (findAllCache) { return findAllCache; } var array = DS.RecordArray.create({ type: type, content: Ember.A([]), store: this }); this.registerRecordArray(array, type); var adapter = get(this, '_adapter'); if (adapter && adapter.findAll) { adapter.findAll(this, type); } typeMap.findAllCache = array; return array; }, filter: function(type, query, filter) { // allow an optional server query if (arguments.length === 3) { this.findQuery(type, query); } else if (arguments.length === 2) { filter = query; } var array = DS.FilteredRecordArray.create({ type: type, content: Ember.A([]), store: this, filterFunction: filter }); this.registerRecordArray(array, type, filter); return array; }, // ............ // . UPDATING . // ............ hashWasUpdated: function(type, clientId, record) { // Because hash updates are invoked at the end of the run loop, // it is possible that a record might be deleted after its hash // has been modified and this method was scheduled to be called. // // If that's the case, the record would have already been removed // from all record arrays; calling updateRecordArrays would just // add it back. If the record is deleted, just bail. It shouldn't // give us any more trouble after this. if (get(record, 'isDeleted')) { return; } this.updateRecordArrays(type, clientId, get(record, 'data')); }, // .............. // . PERSISTING . // .............. commit: function() { var defaultTransaction = get(this, 'defaultTransaction'); set(this, 'defaultTransaction', this.transaction()); defaultTransaction.commit(); }, didUpdateRecords: function(array, hashes) { if (hashes) { array.forEach(function(record, idx) { this.didUpdateRecord(record, hashes[idx]); }, this); } else { array.forEach(function(record) { this.didUpdateRecord(record); }, this); } }, didUpdateRecord: function(record, hash) { if (hash) { var clientId = get(record, 'clientId'), dataCache = this.typeMapFor(record.constructor).cidToHash; dataCache[clientId] = hash; record.send('didChangeData'); record.hashWasUpdated(); } else { record.send('didSaveData'); } record.send('didCommit'); }, didDeleteRecords: function(array) { array.forEach(function(record) { record.send('didCommit'); }); }, didDeleteRecord: function(record) { record.send('didCommit'); }, _didCreateRecord: function(record, hash, typeMap, clientId, primaryKey) { var recordData = get(record, 'data'), id, changes; if (hash) { typeMap.cidToHash[clientId] = hash; // If the server returns a hash, we assume that the server's version // of the data supercedes the local changes. record.beginPropertyChanges(); record.send('didChangeData'); recordData.adapterDidUpdate(); record.hashWasUpdated(); record.endPropertyChanges(); id = hash[primaryKey]; typeMap.idToCid[id] = clientId; this.clientIdToId[clientId] = id; } else { recordData.commit(); } record.send('didCommit'); }, didCreateRecords: function(type, array, hashes) { var primaryKey = type.proto().primaryKey, typeMap = this.typeMapFor(type), clientId; for (var i=0, l=get(array, 'length'); i "created.uncommitted" The `DS.Model` states are themselves stateless. What we mean is that, though each instance of a record also has a unique instance of a `DS.StateManager`, the hierarchical states that each of *those* points to is a shared data structure. For performance reasons, instead of each record getting its own copy of the hierarchy of states, each state manager points to this global, immutable shared instance. How does a state know which record it should be acting on? We pass a reference to the current state manager as the first parameter to every method invoked on a state. The state manager passed as the first parameter is where you should stash state about the record if needed; you should never store data on the state object itself. If you need access to the record being acted on, you can retrieve the state manager's `record` property. For example, if you had an event handler `myEvent`: myEvent: function(manager) { var record = manager.get('record'); record.doSomething(); } For more information about state managers in general, see the Ember.js documentation on `Ember.StateManager`. ### Events, Flags, and Transitions A state may implement zero or more events, flags, or transitions. #### Events Events are named functions that are invoked when sent to a record. The state manager will first look for a method with the given name on the current state. If no method is found, it will search the current state's parent, and then its grandparent, and so on until reaching the top of the hierarchy. If the root is reached without an event handler being found, an exception will be raised. This can be very helpful when debugging new features. Here's an example implementation of a state with a `myEvent` event handler: aState: DS.State.create({ myEvent: function(manager, param) { console.log("Received myEvent with "+param); } }) To trigger this event: record.send('myEvent', 'foo'); //=> "Received myEvent with foo" Note that an optional parameter can be sent to a record's `send()` method, which will be passed as the second parameter to the event handler. Events should transition to a different state if appropriate. This can be done by calling the state manager's `goToState()` method with a path to the desired state. The state manager will attempt to resolve the state path relative to the current state. If no state is found at that path, it will attempt to resolve it relative to the current state's parent, and then its parent, and so on until the root is reached. For example, imagine a hierarchy like this: * created * start <-- currentState * inFlight * updated * inFlight If we are currently in the `start` state, calling `goToState('inFlight')` would transition to the `created.inFlight` state, while calling `goToState('updated.inFlight')` would transition to the `updated.inFlight` state. Remember that *only events* should ever cause a state transition. You should never call `goToState()` from outside a state's event handler. If you are tempted to do so, create a new event and send that to the state manager. #### Flags Flags are Boolean values that can be used to introspect a record's current state in a more user-friendly way than examining its state path. For example, instead of doing this: var statePath = record.getPath('stateManager.currentState.path'); if (statePath === 'created.inFlight') { doSomething(); } You can say: if (record.get('isNew') && record.get('isSaving')) { doSomething(); } If your state does not set a value for a given flag, the value will be inherited from its parent (or the first place in the state hierarchy where it is defined). The current set of flags are defined below. If you want to add a new flag, in addition to the area below, you will also need to declare it in the `DS.Model` class. #### Transitions Transitions are like event handlers but are called automatically upon entering or exiting a state. To implement a transition, just call a method either `enter` or `exit`: myState: DS.State.create({ // Gets called automatically when entering // this state. enter: function(manager) { console.log("Entered myState"); } }) Note that enter and exit events are called once per transition. If the current state changes, but changes to another child state of the parent, the transition event on the parent will not be triggered. */ var stateProperty = Ember.computed(function(key) { var parent = get(this, 'parentState'); if (parent) { return get(parent, key); } }).property(); var isEmptyObject = function(object) { for (var name in object) { if (object.hasOwnProperty(name)) { return false; } } return true; }; var hasDefinedProperties = function(object) { for (var name in object) { if (object.hasOwnProperty(name) && object[name]) { return true; } } return false; }; DS.State = Ember.State.extend({ isLoaded: stateProperty, isDirty: stateProperty, isSaving: stateProperty, isDeleted: stateProperty, isError: stateProperty, isNew: stateProperty, isValid: stateProperty, isPending: stateProperty, // For states that are substates of a // DirtyState (updated or created), it is // useful to be able to determine which // type of dirty state it is. dirtyType: stateProperty }); var setProperty = function(manager, context) { var key = context.key, value = context.value; var record = get(manager, 'record'), data = get(record, 'data'); set(data, key, value); }; var setAssociation = function(manager, context) { var key = context.key, value = context.value; var record = get(manager, 'record'), data = get(record, 'data'); data.setAssociation(key, value); }; var didChangeData = function(manager) { var record = get(manager, 'record'), data = get(record, 'data'); data._savedData = null; record.notifyPropertyChange('data'); }; // The waitingOn event shares common functionality // between the different dirty states, but each is // treated slightly differently. This method is exposed // so that each implementation can invoke the common // behavior, and then implement the behavior specific // to the state. var waitingOn = function(manager, object) { var record = get(manager, 'record'), pendingQueue = get(record, 'pendingQueue'), objectGuid = guidFor(object); var observer = function() { if (get(object, 'id')) { manager.send('doneWaitingOn', object); Ember.removeObserver(object, 'id', observer); } }; pendingQueue[objectGuid] = [object, observer]; Ember.addObserver(object, 'id', observer); }; // Implementation notes: // // Each state has a boolean value for all of the following flags: // // * isLoaded: The record has a populated `data` property. When a // record is loaded via `store.find`, `isLoaded` is false // until the adapter sets it. When a record is created locally, // its `isLoaded` property is always true. // * isDirty: The record has local changes that have not yet been // saved by the adapter. This includes records that have been // created (but not yet saved) or deleted. // * isSaving: The record's transaction has been committed, but // the adapter has not yet acknowledged that the changes have // been persisted to the backend. // * isDeleted: The record was marked for deletion. When `isDeleted` // is true and `isDirty` is true, the record is deleted locally // but the deletion was not yet persisted. When `isSaving` is // true, the change is in-flight. When both `isDirty` and // `isSaving` are false, the change has persisted. // * isError: The adapter reported that it was unable to save // local changes to the backend. This may also result in the // record having its `isValid` property become false if the // adapter reported that server-side validations failed. // * isNew: The record was created on the client and the adapter // did not yet report that it was successfully saved. // * isValid: No client-side validations have failed and the // adapter did not report any server-side validation failures. // * isPending: A record `isPending` when it belongs to an // association on another record and that record has not been // saved. A record in this state cannot be saved because it // lacks a "foreign key" that will be supplied by its parent // association when the parent record has been created. When // the adapter reports that the parent has saved, the // `isPending` property on all children will become `false` // and the transaction will try to commit the records. // This mixin is mixed into various uncommitted states. Make // sure to mix it in *after* the class definition, so its // super points to the class definition. var Uncommitted = Ember.Mixin.create({ setProperty: setProperty, setAssociation: setAssociation, }); // These mixins are mixed into substates of the concrete // subclasses of DirtyState. var CreatedUncommitted = Ember.Mixin.create({ deleteRecord: function(manager) { var record = get(manager, 'record'); this._super(manager); record.withTransaction(function(t) { t.recordBecameClean('created', record); }); manager.goToState('deleted.saved'); } }); var UpdatedUncommitted = Ember.Mixin.create({ deleteRecord: function(manager) { this._super(manager); var record = get(manager, 'record'); record.withTransaction(function(t) { t.recordBecameClean('updated', record); }); manager.goToState('deleted'); } }); // The dirty state is a abstract state whose functionality is // shared between the `created` and `updated` states. // // The deleted state shares the `isDirty` flag with the // subclasses of `DirtyState`, but with a very different // implementation. var DirtyState = DS.State.extend({ initialState: 'uncommitted', // FLAGS isDirty: true, // SUBSTATES // When a record first becomes dirty, it is `uncommitted`. // This means that there are local pending changes, // but they have not yet begun to be saved. uncommitted: DS.State.extend({ // TRANSITIONS enter: function(manager) { var dirtyType = get(this, 'dirtyType'), record = get(manager, 'record'); record.withTransaction(function (t) { t.recordBecameDirty(dirtyType, record); }); }, // EVENTS deleteRecord: Ember.K, waitingOn: function(manager, object) { waitingOn(manager, object); manager.goToState('pending'); }, willCommit: function(manager) { manager.goToState('inFlight'); }, becameInvalid: function(manager) { var dirtyType = get(this, 'dirtyType'), record = get(manager, 'record'); record.withTransaction(function (t) { t.recordBecameInFlight(dirtyType, record); }); manager.goToState('invalid'); }, rollback: function(manager) { var record = get(manager, 'record'), dirtyType = get(this, 'dirtyType'), data = get(record, 'data'); data.rollback(); record.withTransaction(function(t) { t.recordBecameClean(dirtyType, record); }); manager.goToState('loaded'); } }, Uncommitted), // Once a record has been handed off to the adapter to be // saved, it is in the 'in flight' state. Changes to the // record cannot be made during this window. inFlight: DS.State.extend({ // FLAGS isSaving: true, // TRANSITIONS enter: function(manager) { var dirtyType = get(this, 'dirtyType'), record = get(manager, 'record'); record.withTransaction(function (t) { t.recordBecameInFlight(dirtyType, record); }); }, // EVENTS didCommit: function(manager) { var dirtyType = get(this, 'dirtyType'), record = get(manager, 'record'); record.withTransaction(function(t) { t.recordBecameClean('inflight', record); }); manager.goToState('loaded'); manager.send('invokeLifecycleCallbacks', dirtyType); }, becameInvalid: function(manager, errors) { var record = get(manager, 'record'); set(record, 'errors', errors); manager.goToState('invalid'); manager.send('invokeLifecycleCallbacks'); }, becameError: function(manager) { manager.goToState('error'); manager.send('invokeLifecycleCallbacks'); }, didChangeData: didChangeData }), // If a record becomes associated with a newly created // parent record, it will be `pending` until the parent // record has successfully persisted. Once this happens, // this record can use the parent's primary key as its // foreign key. // // If the record's transaction had already started to // commit, the record will transition to the `inFlight` // state. If it had not, the record will transition to // the `uncommitted` state. pending: DS.State.extend({ initialState: 'uncommitted', // FLAGS isPending: true, // SUBSTATES // A pending record whose transaction has not yet // started to commit is in this state. uncommitted: DS.State.extend({ // EVENTS deleteRecord: function(manager) { var record = get(manager, 'record'), pendingQueue = get(record, 'pendingQueue'), tuple; // since we are leaving the pending state, remove any // observers we have registered on other records. for (var prop in pendingQueue) { if (!pendingQueue.hasOwnProperty(prop)) { continue; } tuple = pendingQueue[prop]; Ember.removeObserver(tuple[0], 'id', tuple[1]); } }, willCommit: function(manager) { manager.goToState('committing'); }, doneWaitingOn: function(manager, object) { var record = get(manager, 'record'), pendingQueue = get(record, 'pendingQueue'), objectGuid = guidFor(object); delete pendingQueue[objectGuid]; if (isEmptyObject(pendingQueue)) { manager.send('doneWaiting'); } }, doneWaiting: function(manager) { var dirtyType = get(this, 'dirtyType'); manager.goToState(dirtyType + '.uncommitted'); } }, Uncommitted), // A pending record whose transaction has started // to commit is in this state. Since it has not yet // been sent to the adapter, it is not `inFlight` // until all of its dependencies have been committed. committing: DS.State.extend({ // FLAGS isSaving: true, // EVENTS doneWaitingOn: function(manager, object) { var record = get(manager, 'record'), pendingQueue = get(record, 'pendingQueue'), objectGuid = guidFor(object); delete pendingQueue[objectGuid]; if (isEmptyObject(pendingQueue)) { manager.send('doneWaiting'); } }, doneWaiting: function(manager) { var record = get(manager, 'record'), transaction = get(record, 'transaction'); // Now that the record is no longer pending, schedule // the transaction to commit. Ember.run.once(transaction, transaction.commit); }, willCommit: function(manager) { var record = get(manager, 'record'), pendingQueue = get(record, 'pendingQueue'); if (isEmptyObject(pendingQueue)) { var dirtyType = get(this, 'dirtyType'); manager.goToState(dirtyType + '.inFlight'); } } }) }), // A record is in the `invalid` state when its client-side // invalidations have failed, or if the adapter has indicated // the the record failed server-side invalidations. invalid: DS.State.extend({ // FLAGS isValid: false, exit: function(manager) { var record = get(manager, 'record'); record.withTransaction(function (t) { t.recordBecameClean('inflight', record); }); }, // EVENTS deleteRecord: function(manager) { manager.goToState('deleted'); }, setAssociation: setAssociation, setProperty: function(manager, context) { setProperty(manager, context); var record = get(manager, 'record'), errors = get(record, 'errors'), key = context.key; delete errors[key]; if (!hasDefinedProperties(errors)) { manager.send('becameValid'); } }, rollback: function(manager) { manager.send('becameValid'); manager.send('rollback'); }, becameValid: function(manager) { manager.goToState('uncommitted'); }, invokeLifecycleCallbacks: function(manager) { var record = get(manager, 'record'); record.fire('becameInvalid', record); } }) }); // The created and updated states are created outside the state // chart so we can reopen their substates and add mixins as // necessary. var createdState = DirtyState.create({ dirtyType: 'created', // FLAGS isNew: true }); var updatedState = DirtyState.create({ dirtyType: 'updated' }); // The created.uncommitted state and created.pending.uncommitted share // some logic defined in CreatedUncommitted. createdState.states.uncommitted.reopen(CreatedUncommitted); createdState.states.pending.states.uncommitted.reopen(CreatedUncommitted); // The created.uncommitted state needs to immediately transition to the // deleted state if it is rolled back. createdState.states.uncommitted.reopen({ rollback: function(manager) { this._super(manager); manager.goToState('deleted.saved'); } }); // The updated.uncommitted state and updated.pending.uncommitted share // some logic defined in UpdatedUncommitted. updatedState.states.uncommitted.reopen(UpdatedUncommitted); updatedState.states.pending.states.uncommitted.reopen(UpdatedUncommitted); updatedState.states.inFlight.reopen({ didSaveData: function(manager) { var record = get(manager, 'record'), data = get(record, 'data'); data.saveData(); data.adapterDidUpdate(); } }); var states = { rootState: Ember.State.create({ // FLAGS isLoaded: false, isDirty: false, isSaving: false, isDeleted: false, isError: false, isNew: false, isValid: true, isPending: false, // SUBSTATES // A record begins its lifecycle in the `empty` state. // If its data will come from the adapter, it will // transition into the `loading` state. Otherwise, if // the record is being created on the client, it will // transition into the `created` state. empty: DS.State.create({ // EVENTS loadingData: function(manager) { manager.goToState('loading'); }, didChangeData: function(manager) { didChangeData(manager); manager.goToState('loaded.created'); } }), // A record enters this state when the store askes // the adapter for its data. It remains in this state // until the adapter provides the requested data. // // Usually, this process is asynchronous, using an // XHR to retrieve the data. loading: DS.State.create({ // TRANSITIONS exit: function(manager) { var record = get(manager, 'record'); record.fire('didLoad'); }, // EVENTS didChangeData: function(manager, data) { didChangeData(manager); manager.send('loadedData'); }, loadedData: function(manager) { manager.goToState('loaded'); } }), // A record enters this state when its data is populated. // Most of a record's lifecycle is spent inside substates // of the `loaded` state. loaded: DS.State.create({ initialState: 'saved', // FLAGS isLoaded: true, // SUBSTATES // If there are no local changes to a record, it remains // in the `saved` state. saved: DS.State.create({ // EVENTS setProperty: function(manager, context) { setProperty(manager, context); manager.goToState('updated'); }, setAssociation: function(manager, context) { setAssociation(manager, context); manager.goToState('updated'); }, didChangeData: didChangeData, deleteRecord: function(manager) { manager.goToState('deleted'); }, waitingOn: function(manager, object) { waitingOn(manager, object); manager.goToState('updated.pending'); }, invokeLifecycleCallbacks: function(manager, dirtyType) { var record = get(manager, 'record'); if (dirtyType === 'created') { record.fire('didCreate', record); } else { record.fire('didUpdate', record); } } }), // A record is in this state after it has been locally // created but before the adapter has indicated that // it has been saved. created: createdState, // A record is in this state if it has already been // saved to the server, but there are new local changes // that have not yet been saved. updated: updatedState }), // A record is in this state if it was deleted from the store. deleted: DS.State.create({ // FLAGS isDeleted: true, isLoaded: true, isDirty: true, // TRANSITIONS enter: function(manager) { var record = get(manager, 'record'), store = get(record, 'store'); store.removeFromRecordArrays(record); }, // SUBSTATES // When a record is deleted, it enters the `start` // state. It will exit this state when the record's // transaction starts to commit. start: DS.State.create({ // TRANSITIONS enter: function(manager) { var record = get(manager, 'record'); record.withTransaction(function(t) { t.recordBecameDirty('deleted', record); }); }, // EVENTS willCommit: function(manager) { manager.goToState('inFlight'); }, rollback: function(manager) { var record = get(manager, 'record'), data = get(record, 'data'); data.rollback(); record.withTransaction(function(t) { t.recordBecameClean('deleted', record); }); manager.goToState('loaded'); } }), // After a record's transaction is committing, but // before the adapter indicates that the deletion // has saved to the server, a record is in the // `inFlight` substate of `deleted`. inFlight: DS.State.create({ // FLAGS isSaving: true, // TRANSITIONS enter: function(manager) { var record = get(manager, 'record'); record.withTransaction(function (t) { t.recordBecameInFlight('deleted', record); }); }, // EVENTS didCommit: function(manager) { var record = get(manager, 'record'); record.withTransaction(function(t) { t.recordBecameClean('inflight', record); }); manager.goToState('saved'); manager.send('invokeLifecycleCallbacks'); } }), // Once the adapter indicates that the deletion has // been saved, the record enters the `saved` substate // of `deleted`. saved: DS.State.create({ // FLAGS isDirty: false, invokeLifecycleCallbacks: function(manager) { var record = get(manager, 'record'); record.fire('didDelete', record); } }) }), // If the adapter indicates that there was an unknown // error saving a record, the record enters the `error` // state. error: DS.State.create({ isError: true, // EVENTS invokeLifecycleCallbacks: function(manager) { var record = get(manager, 'record'); record.fire('becameError', record); } }) }) }; DS.StateManager = Ember.StateManager.extend({ record: null, initialState: 'rootState', states: states }); })(); (function() { var get = Ember.get, set = Ember.set; // When a record is changed on the client, it is considered "dirty"--there are // pending changes that need to be saved to a persistence layer, such as a // server. // // If the record is rolled back, it re-enters a clean state, any changes are // discarded, and its attributes are reset back to the last known good copy // of the data that came from the server. // // If the record is committed, the changes are sent to the server to be saved, // and once the server confirms that they are valid, the record's "canonical" // data becomes the original canonical data plus the changes merged in. // // A DataProxy is an object that encapsulates this change tracking. It // contains three buckets: // // * `savedData` - the last-known copy of the data from the server // * `unsavedData` - a hash that contains any changes that have not yet // been committed // * `associations` - this is similar to `savedData`, but holds the client // ids of associated records // // When setting a property on the object, the value is placed into the // `unsavedData` bucket: // // proxy.set('key', 'value'); // // // unsavedData: // { // key: "value" // } // // When retrieving a property from the object, it first looks to see // if that value exists in the `unsavedData` bucket, and returns it if so. // Otherwise, it returns the value from the `savedData` bucket. // // When the adapter notifies a record that it has been saved, it merges the // `unsavedData` bucket into the `savedData` bucket. If the record's // transaction is rolled back, the `unsavedData` hash is simply discarded. // // This object is a regular JS object for performance. It is only // used internally for bookkeeping purposes. var DataProxy = DS._DataProxy = function(record) { this.record = record; this.unsavedData = {}; this.associations = {}; }; DataProxy.prototype = { get: function(key) { return Ember.get(this, key); }, set: function(key, value) { return Ember.set(this, key, value); }, setAssociation: function(key, value) { this.associations[key] = value; }, savedData: function() { var savedData = this._savedData; if (savedData) { return savedData; } var record = this.record, clientId = get(record, 'clientId'), store = get(record, 'store'); if (store) { savedData = store.dataForRecord(record); this._savedData = savedData; return savedData; } }, unknownProperty: function(key) { var unsavedData = this.unsavedData, associations = this.associations, savedData = this.savedData(), store; var value = unsavedData[key], association; // if this is a belongsTo association, this will // be a clientId. association = associations[key]; if (association !== undefined) { store = get(this.record, 'store'); return store.clientIdToId[association]; } if (savedData && value === undefined) { value = savedData[key]; } return value; }, setUnknownProperty: function(key, value) { var record = this.record, unsavedData = this.unsavedData; unsavedData[key] = value; record.hashWasUpdated(); return value; }, commit: function() { this.saveData(); this.record.notifyPropertyChange('data'); }, rollback: function() { this.unsavedData = {}; this.record.notifyPropertyChange('data'); }, saveData: function() { var record = this.record; var unsavedData = this.unsavedData; var savedData = this.savedData(); for (var prop in unsavedData) { if (unsavedData.hasOwnProperty(prop)) { savedData[prop] = unsavedData[prop]; delete unsavedData[prop]; } } }, adapterDidUpdate: function() { this.unsavedData = {}; } }; })(); (function() { var get = Ember.get, set = Ember.set, getPath = Ember.getPath, none = Ember.none; var retrieveFromCurrentState = Ember.computed(function(key) { return get(getPath(this, 'stateManager.currentState'), key); }).property('stateManager.currentState').cacheable(); DS.Model = Ember.Object.extend(Ember.Evented, { isLoaded: retrieveFromCurrentState, isDirty: retrieveFromCurrentState, isSaving: retrieveFromCurrentState, isDeleted: retrieveFromCurrentState, isError: retrieveFromCurrentState, isNew: retrieveFromCurrentState, isPending: retrieveFromCurrentState, isValid: retrieveFromCurrentState, clientId: null, transaction: null, stateManager: null, pendingQueue: null, errors: null, // because unknownProperty is used, any internal property // must be initialized here. primaryKey: 'id', id: Ember.computed(function(key, value) { var primaryKey = get(this, 'primaryKey'), data = get(this, 'data'); if (arguments.length === 2) { set(data, primaryKey, value); return value; } return data && get(data, primaryKey); }).property('primaryKey', 'data'), // The following methods are callbacks invoked by `toJSON`. You // can override one of the callbacks to override specific behavior, // or toJSON itself. // // If you override toJSON, you can invoke these callbacks manually // to get the default behavior. /** Add the record's primary key to the JSON hash. The default implementation uses the record's specified `primaryKey` and the `id` computed property, which are passed in as parameters. @param {Object} json the JSON hash being built @param {Number|String} id the record's id @param {String} key the primaryKey for the record */ addIdToJSON: function(json, id, key) { if (id) { json[key] = id; } }, /** Add the attributes' current values to the JSON hash. The default implementation gets the current value of each attribute from the `data`, and uses a `defaultValue` if specified in the `DS.attr` definition. @param {Object} json the JSON hash being build @param {Ember.Map} attributes a Map of attributes @param {DataProxy} data the record's data, accessed with `get` and `set`. */ addAttributesToJSON: function(json, attributes, data) { attributes.forEach(function(name, meta) { var key = meta.key(this.constructor), value = get(data, key); if (value === undefined) { value = meta.options.defaultValue; } json[key] = value; }, this); }, /** Add the value of a `hasMany` association to the JSON hash. The default implementation honors the `embedded` option passed to `DS.hasMany`. If embedded, `toJSON` is recursively called on the child records. If not, the `id` of each record is added. Note that if a record is not embedded and does not yet have an `id` (usually provided by the server), it will not be included in the output. @param {Object} json the JSON hash being built @param {DataProxy} data the record's data, accessed with `get` and `set`. @param {Object} meta information about the association @param {Object} options options passed to `toJSON` */ addHasManyToJSON: function(json, data, meta, options) { var key = meta.key, manyArray = get(this, key), records = [], i, l, clientId, id; if (meta.options.embedded) { // TODO: Avoid materializing embedded hashes if possible manyArray.forEach(function(record) { records.push(record.toJSON(options)); }); } else { var clientIds = get(manyArray, 'content'); for (i=0, l=clientIds.length; i