|
1 | 1 | import Ember from 'ember';
|
2 | 2 | import DS from 'ember-data';
|
3 | 3 | import { pluralize } from 'ember-inflector';
|
| 4 | +import { v4 } from 'uuid'; |
4 | 5 | //import BelongsToRelationship from 'ember-data/-private/system/relationships/state/belongs-to';
|
5 | 6 |
|
6 | 7 | import {
|
@@ -153,7 +154,7 @@ export default DS.RESTAdapter.extend({
|
153 | 154 | willDestroy: function() {
|
154 | 155 | this._stopChangesListener();
|
155 | 156 | },
|
156 |
| - |
| 157 | + |
157 | 158 | _indexPromises: [],
|
158 | 159 |
|
159 | 160 | _init: function (store, type) {
|
@@ -206,8 +207,9 @@ export default DS.RESTAdapter.extend({
|
206 | 207 | relModel = (typeof rel.type === 'string' ? store.modelFor(rel.type) : rel.type);
|
207 | 208 | if (relModel) {
|
208 | 209 | let includeRel = true;
|
209 |
| - if (!('options' in rel)) rel.options = {}; |
210 |
| - |
| 210 | + if (!('options' in rel)) { |
| 211 | + rel.options = {}; |
| 212 | + } |
211 | 213 | if (typeof(rel.options.async) === "undefined") {
|
212 | 214 | rel.options.async = config.emberPouch && !Ember.isEmpty(config.emberPouch.async) ? config.emberPouch.async : true;//default true from https://github.com/emberjs/data/pull/3366
|
213 | 215 | }
|
@@ -464,27 +466,46 @@ export default DS.RESTAdapter.extend({
|
464 | 466 | });
|
465 | 467 | },
|
466 | 468 |
|
| 469 | + generateIdForRecord: function(/* store, type, inputProperties */) { |
| 470 | + return v4(); |
| 471 | + }, |
| 472 | + |
467 | 473 | createdRecords: {},
|
468 |
| - createRecord: function(store, type, record) { |
469 |
| - this._init(store, type); |
470 |
| - var data = this._recordToData(store, type, record); |
471 |
| - let rel = this.get('db').rel; |
472 |
| - |
473 |
| - let id = data.id; |
474 |
| - if (!id) { |
475 |
| - id = data.id = rel.uuid(); |
| 474 | + createRecord: function(store, type, snapshot) { |
| 475 | + const record = snapshot.record; |
| 476 | + if (record._emberPouchSavePromise) { |
| 477 | + const changes = record.changedAttributes(); |
| 478 | + record._emberPouchSavePromise = record._emberPouchSavePromise.then(records => { |
| 479 | + // If there have been changes since the document was created then we should update the record now |
| 480 | + if (Object.keys(changes).length > 0) { |
| 481 | + const rev = records[Object.keys(records)[0]][0].rev; |
| 482 | + (snapshot.__attributes || snapshot._attributes).rev = rev; // FIXME: it should be possible to do this elsewhere |
| 483 | + return this.updateRecord(store, type, snapshot); |
| 484 | + } |
| 485 | + return records; |
| 486 | + }); |
| 487 | + return record._emberPouchSavePromise; |
476 | 488 | }
|
| 489 | + |
| 490 | + this._init(store, type); |
| 491 | + var data = this._recordToData(store, type, snapshot); |
| 492 | + const rel = this.get('db').rel; |
| 493 | + const id = data.id; |
477 | 494 | this.createdRecords[id] = true;
|
478 |
| - |
479 |
| - return rel.save(this.getRecordTypeName(type), data).catch((e) => { |
480 |
| - delete this.createdRecords[id]; |
481 |
| - throw e; |
| 495 | + Object.defineProperty(record, '_emberPouchSavePromise', { |
| 496 | + enumerable: false, |
| 497 | + writable: true, |
| 498 | + value: rel.save(this.getRecordTypeName(type), data).catch((e) => { |
| 499 | + delete this.createdRecords[id]; |
| 500 | + throw e; |
| 501 | + }), |
482 | 502 | });
|
| 503 | + return record._emberPouchSavePromise; |
483 | 504 | },
|
484 | 505 |
|
485 |
| - updateRecord: function (store, type, record) { |
| 506 | + updateRecord: function (store, type, snapshot) { |
486 | 507 | this._init(store, type);
|
487 |
| - var data = this._recordToData(store, type, record); |
| 508 | + var data = this._recordToData(store, type, snapshot); |
488 | 509 | return this.get('db').rel.save(this.getRecordTypeName(type), data);
|
489 | 510 | },
|
490 | 511 |
|
|
0 commit comments