-
-
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.
Merge pull request #3303 from pangratz/reference_unification
Implement RFC 57 - Reference Unification
- Loading branch information
Showing
16 changed files
with
1,917 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import RecordReference from './references/record'; | ||
import BelongsToReference from './references/belongs-to'; | ||
import HasManyReference from './references/has-many'; | ||
|
||
export { RecordReference, BelongsToReference, HasManyReference }; |
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,82 @@ | ||
import Model from 'ember-data/-private/system/model'; | ||
import Ember from 'ember'; | ||
import Reference from './reference'; | ||
|
||
import { assertPolymorphicType } from "ember-data/-private/utils"; | ||
|
||
var BelongsToReference = function(store, parentInternalModel, belongsToRelationship) { | ||
this._super$constructor(store, parentInternalModel); | ||
this.belongsToRelationship = belongsToRelationship; | ||
this.type = belongsToRelationship.relationshipMeta.type; | ||
this.parent = parentInternalModel.recordReference; | ||
|
||
// TODO inverse | ||
}; | ||
|
||
BelongsToReference.prototype = Object.create(Reference.prototype); | ||
BelongsToReference.prototype.constructor = BelongsToReference; | ||
BelongsToReference.prototype._super$constructor = Reference; | ||
|
||
BelongsToReference.prototype.remoteType = function() { | ||
if (this.belongsToRelationship.link) { | ||
return "link"; | ||
} | ||
|
||
return "id"; | ||
}; | ||
|
||
BelongsToReference.prototype.id = function() { | ||
var inverseRecord = this.belongsToRelationship.inverseRecord; | ||
return inverseRecord && inverseRecord.id; | ||
}; | ||
|
||
BelongsToReference.prototype.link = function() { | ||
return this.belongsToRelationship.link; | ||
}; | ||
|
||
BelongsToReference.prototype.meta = function() { | ||
return this.belongsToRelationship.meta; | ||
}; | ||
|
||
BelongsToReference.prototype.push = function(objectOrPromise) { | ||
return Ember.RSVP.resolve(objectOrPromise).then((data) => { | ||
var record; | ||
|
||
if (data instanceof Model) { | ||
record = data; | ||
} else { | ||
record = this.store.push(data); | ||
} | ||
|
||
assertPolymorphicType(this.internalModel, this.belongsToRelationship.relationshipMeta, record._internalModel); | ||
|
||
this.belongsToRelationship.setCanonicalRecord(record._internalModel); | ||
|
||
return record; | ||
}); | ||
}; | ||
|
||
BelongsToReference.prototype.value = function() { | ||
var inverseRecord = this.belongsToRelationship.inverseRecord; | ||
return inverseRecord && inverseRecord.record; | ||
}; | ||
|
||
BelongsToReference.prototype.load = function() { | ||
if (this.remoteType() === "id") { | ||
return this.belongsToRelationship.getRecord(); | ||
} | ||
|
||
if (this.remoteType() === "link") { | ||
return this.belongsToRelationship.findLink().then((internalModel) => { | ||
return this.value(); | ||
}); | ||
} | ||
}; | ||
|
||
BelongsToReference.prototype.reload = function() { | ||
return this.belongsToRelationship.reload().then((internalModel) => { | ||
return this.value(); | ||
}); | ||
}; | ||
|
||
export default BelongsToReference; |
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,99 @@ | ||
import Ember from 'ember'; | ||
import Reference from './reference'; | ||
|
||
const get = Ember.get; | ||
|
||
var HasManyReference = function(store, parentInternalModel, hasManyRelationship) { | ||
this._super$constructor(store, parentInternalModel); | ||
this.hasManyRelationship = hasManyRelationship; | ||
this.type = hasManyRelationship.relationshipMeta.type; | ||
this.parent = parentInternalModel.recordReference; | ||
|
||
// TODO inverse | ||
}; | ||
|
||
HasManyReference.prototype = Object.create(Reference.prototype); | ||
HasManyReference.prototype.constructor = HasManyReference; | ||
HasManyReference.prototype._super$constructor = Reference; | ||
|
||
HasManyReference.prototype.remoteType = function() { | ||
if (this.hasManyRelationship.link) { | ||
return "link"; | ||
} | ||
|
||
return "ids"; | ||
}; | ||
|
||
HasManyReference.prototype.link = function() { | ||
return this.hasManyRelationship.link; | ||
}; | ||
|
||
HasManyReference.prototype.ids = function() { | ||
var members = this.hasManyRelationship.members; | ||
var ids = members.toArray().map(function(internalModel) { | ||
return internalModel.id; | ||
}); | ||
|
||
return ids; | ||
}; | ||
|
||
HasManyReference.prototype.meta = function() { | ||
return this.hasManyRelationship.manyArray.meta; | ||
}; | ||
|
||
HasManyReference.prototype.push = function(objectOrPromise) { | ||
return Ember.RSVP.resolve(objectOrPromise).then((payload) => { | ||
var array = payload; | ||
if (typeof payload === "object" && payload.data) { | ||
array = payload.data; | ||
} | ||
|
||
var internalModels = array.map((obj) => { | ||
var record = this.store.push(obj); | ||
return record._internalModel; | ||
}); | ||
|
||
// TODO add assertion for polymorphic type | ||
|
||
this.hasManyRelationship.computeChanges(internalModels); | ||
|
||
return this.hasManyRelationship.manyArray; | ||
}); | ||
}; | ||
|
||
HasManyReference.prototype._isLoaded = function() { | ||
var hasData = get(this.hasManyRelationship, 'hasData'); | ||
if (!hasData) { | ||
return false; | ||
} | ||
|
||
var members = this.hasManyRelationship.members.toArray(); | ||
var isEveryLoaded = members.every(function(internalModel) { | ||
return internalModel.isLoaded() === true; | ||
}); | ||
|
||
return isEveryLoaded; | ||
}; | ||
|
||
HasManyReference.prototype.value = function() { | ||
if (this._isLoaded()) { | ||
return this.hasManyRelationship.manyArray; | ||
} | ||
|
||
return null; | ||
}; | ||
|
||
HasManyReference.prototype.load = function() { | ||
if (!this._isLoaded()) { | ||
return this.hasManyRelationship.getRecords(); | ||
} | ||
|
||
var manyArray = this.hasManyRelationship.manyArray; | ||
return Ember.RSVP.resolve(manyArray); | ||
}; | ||
|
||
HasManyReference.prototype.reload = function() { | ||
return this.hasManyRelationship.reload(); | ||
}; | ||
|
||
export default HasManyReference; |
Oops, something went wrong.