Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the API docs for snapshots #4638

Merged
merged 1 commit into from
Jan 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(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(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(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(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(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(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