-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CHORE] Move PromiseBelongsTo, PromiseManyArray and ManyArray to mode…
…l package (#6822) * [CHORE] Move PromiseBelongsTo, PromiseManyArray and ManyArray to model package * fixes * reduce inter-dependency by moving proxyToContent * cleanup
- Loading branch information
Showing
10 changed files
with
141 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
packages/model/addon/-private/system/promise-belongs-to.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { computed } from '@ember/object'; | ||
import { assert } from '@ember/debug'; | ||
import { PromiseObject } from '@ember-data/store/-private'; | ||
|
||
/** | ||
@module @ember-data/model | ||
*/ | ||
|
||
/** | ||
A PromiseBelongsTo is a PromiseObject that also proxies certain method calls | ||
to the underlying belongsTo model. | ||
Right now we proxy: | ||
* `reload()` | ||
@class PromiseBelongsTo | ||
@extends PromiseObject | ||
@private | ||
*/ | ||
const PromiseBelongsTo = PromiseObject.extend({ | ||
// we don't proxy meta because we would need to proxy it to the relationship state container | ||
// however, meta on relationships does not trigger change notifications. | ||
// if you need relationship meta, you should do `record.belongsTo(relationshipName).meta()` | ||
meta: computed(function() { | ||
assert( | ||
'You attempted to access meta on the promise for the async belongsTo relationship ' + | ||
`${this.get('_belongsToState').modelName}:${this.get('_belongsToState').key}'.` + | ||
'\nUse `record.belongsTo(relationshipName).meta()` instead.', | ||
false | ||
); | ||
}), | ||
|
||
reload(options) { | ||
assert('You are trying to reload an async belongsTo before it has been created', this.get('content') !== undefined); | ||
let { key, store, originatingInternalModel } = this._belongsToState; | ||
return store.reloadBelongsTo(this, originatingInternalModel, key, options).then(() => this); | ||
}, | ||
}); | ||
|
||
export default PromiseBelongsTo; |
56 changes: 56 additions & 0 deletions
56
packages/model/addon/-private/system/promise-many-array.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { get } from '@ember/object'; | ||
import { reads } from '@ember/object/computed'; | ||
import { Promise } from 'rsvp'; | ||
import { assert } from '@ember/debug'; | ||
import { FULL_LINKS_ON_RELATIONSHIPS } from '@ember-data/canary-features'; | ||
import { PromiseArray } from '@ember-data/store/-private'; | ||
|
||
/** | ||
@module @ember-data/model | ||
*/ | ||
|
||
/** | ||
A PromiseManyArray is a PromiseArray that also proxies certain method calls | ||
to the underlying manyArray. | ||
Right now we proxy: | ||
* `reload()` | ||
* `createRecord()` | ||
* `on()` | ||
* `one()` | ||
* `trigger()` | ||
* `off()` | ||
* `has()` | ||
@class PromiseManyArray | ||
@extends Ember.ArrayProxy | ||
@private | ||
*/ | ||
const PromiseManyArray = PromiseArray.extend({ | ||
links: FULL_LINKS_ON_RELATIONSHIPS ? reads('content.links') : undefined, | ||
reload(options) { | ||
assert('You are trying to reload an async manyArray before it has been created', get(this, 'content')); | ||
this.set('promise', this.get('content').reload(options)); | ||
return this; | ||
}, | ||
createRecord: proxyToContent('createRecord'), | ||
on: proxyToContent('on'), | ||
one: proxyToContent('one'), | ||
trigger: proxyToContent('trigger'), | ||
off: proxyToContent('off'), | ||
has: proxyToContent('has'), | ||
}); | ||
|
||
export default PromiseManyArray; | ||
|
||
export function promiseManyArray(promise, label) { | ||
return PromiseManyArray.create({ | ||
promise: Promise.resolve(promise, label), | ||
}); | ||
} | ||
|
||
function proxyToContent(method) { | ||
return function() { | ||
return get(this, 'content')[method](...arguments); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters