Skip to content

Commit

Permalink
Update the API docs for snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
bmac committed Nov 10, 2016
1 parent 1ca87a2 commit f1b3f71
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 3 deletions.
93 changes: 91 additions & 2 deletions addon/-private/system/snapshot-record-array.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,125 @@ export default function SnapshotRecordArray(recordArray, meta, options = {}) {
@type {Array}
*/
this._recordArray = recordArray;

/**
Number of records in the array
Example
```app/adapters/post.js
import DS from 'ember-data'
export default DS.JSONAPIAdapter.extend({
shouldReloadAll: function(store, snapshotRecordArray) {
return !snapshotRecordArray.length;
},
});
```
@property length
@type {Number}
*/
this.length = recordArray.get('length');

/**
The type of the underlying records for the snapshots in the array, as a DS.Model
@property type
@type {DS.Model}
*/
this.type = recordArray.get('type');

/**
Meta object
Meta objects for the record array.
Example
```app/adapters/post.js
import DS from 'ember-data'
export default DS.JSONAPIAdapter.extend({
shouldReloadAll: function(store, snapshotRecordArray) {
var lastRequestTime = snapshotRecordArray.meta.lastRequestTime;
var twentyMinutes = 20 * 60 * 1000;
return Date.now() > lastRequestTime + twentyMinutes;
},
});
```
@property meta
@type {Object}
*/
this.meta = meta;

/**
A hash of adapter options
A hash of adapter options passed into the store method for this request.
Example
```app/adapters/post.js
import MyCustomAdapter from './custom-adapter';
export default MyCustomAdapter.extend({
findAll: function(store, type, sinceToken, snapshotRecordArray) {
if (snapshotRecordArray.adapterOptions.subscribe) {
// ...
}
// ...
}
});
```
@property adapterOptions
@type {Object}
*/
this.adapterOptions = options.adapterOptions;

/**
The relationships to include for this request.
Example
```app/adapters/application.js
import DS from 'ember-data';
export default DS.Adapter.extend({
findAll: function(store, type, snapshotRecordArray) {
var url = `/${type.modelName}?include=${encodeURIComponent(snapshotRecordArray.include)}`;
return fetch(url).then((response) => response.json())
}
});
@property include
@type {String|Array}
*/
this.include = options.include;
}

/**
Get snapshots of the underlying record array
Example
```app/adapters/post.js
import DS from 'ember-data'
export default DS.JSONAPIAdapter.extend({
shouldReloadAll: function(store, snapshotArray) {
var snapshots = snapshotArray.snapshots();
return snapshots.any(function(ticketSnapshot) {
var timeDiff = moment().diff(ticketSnapshot.attr('lastAccessedAt'), 'minutes');
if (timeDiff > 20) {
return true;
} else {
return false;
}
});
}
});
```
@method snapshots
@return {Array} Array of snapshots
*/
Expand Down
20 changes: 20 additions & 0 deletions addon/-private/system/snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,26 @@ Snapshot.prototype = {
},

/**
Serializes the snapshot using the serializer for the model.
Example
```app/adapters/application.js
import DS from 'ember-data';
export default DS.Adapter.extend({
createRecord: function(store, type, snapshot) {
var data = snapshot.serialize({ includeId: true });
var url = `/${type.modelName}`;
return fetch(url, {
method: 'POST',
body: data,
}).then((response) => response.json())
}
});
```
@method serialize
@param {Object} options
@return {Object} an object whose values are primitive JSON values only
Expand Down
1 change: 0 additions & 1 deletion addon/-private/system/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,6 @@ Store = Service.extend({
});
```
See [peekAll](#method_peekAll) to get an array of current records in the
store, without waiting until a reload is finished.
Expand Down

0 comments on commit f1b3f71

Please sign in to comment.