Skip to content

Commit

Permalink
Merge pull request #4347 from pangratz/cleanup-ds-references
Browse files Browse the repository at this point in the history
[CLEANUP ds-references]
  • Loading branch information
bmac committed Apr 28, 2016
2 parents e319cc0 + 29a5fa9 commit f699952
Show file tree
Hide file tree
Showing 10 changed files with 1,309 additions and 1,384 deletions.
4 changes: 0 additions & 4 deletions FEATURES.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ entry in `config/features.json`.
`methodForRequest`, `urlForRequest`, `headersForRequest` and `bodyForRequest`
in the `DS.RESTAdapter`.

- `ds-references`

Adds references as described in [RFC 57](https://github.com/emberjs/rfcs/pull/57)

- `ds-transform-pass-options`

Pass options specified for a `DS.attr` to the `DS.Tranform`'s `serialize` and
Expand Down
13 changes: 4 additions & 9 deletions addon/-private/system/model/internal-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import RootState from "ember-data/-private/system/model/states";
import Relationships from "ember-data/-private/system/relationships/state/create";
import Snapshot from "ember-data/-private/system/snapshot";
import EmptyObject from "ember-data/-private/system/empty-object";
import isEnabled from "ember-data/-private/features";

import {
getOwner
Expand Down Expand Up @@ -831,12 +830,9 @@ InternalModel.prototype = {
} else {
return `<${this.modelName}:${this.id}>`;
}
}
};

if (isEnabled('ds-references')) {
},

InternalModel.prototype.referenceFor = function(type, name) {
referenceFor(type, name) {
var reference = this.references[name];

if (!reference) {
Expand All @@ -852,6 +848,5 @@ if (isEnabled('ds-references')) {
}

return reference;
};

}
}
};
244 changes: 115 additions & 129 deletions addon/-private/system/model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import Ember from 'ember';
import { assert, deprecate } from "ember-data/-private/debug";
import { PromiseObject } from "ember-data/-private/system/promise-proxies";
import Errors from "ember-data/-private/system/model/errors";
import isEnabled from 'ember-data/-private/features';
import DebuggerInfoMixin from 'ember-data/-private/system/debug/debug-info';
import { BelongsToMixin } from 'ember-data/-private/system/relationships/belongs-to';
import { HasManyMixin } from 'ember-data/-private/system/relationships/has-many';
Expand Down Expand Up @@ -784,12 +783,123 @@ var Model = Ember.Object.extend(Ember.Evented, {
assert("The `attr` method is not available on DS.Model, a DS.Snapshot was probably expected. Are you passing a DS.Model instead of a DS.Snapshot to your serializer?", false);
},

belongsTo() {
assert("The `belongsTo` method is not available on DS.Model, a DS.Snapshot was probably expected. Are you passing a DS.Model instead of a DS.Snapshot to your serializer?", false);
/**
Get the reference for the specified belongsTo relationship.
Example
```javascript
// models/blog.js
export default DS.Model.extend({
user: DS.belongsTo({ async: true })
});
var blog = store.push({
type: 'blog',
id: 1,
relationships: {
user: { type: 'user', id: 1 }
}
});
var userRef = blog.belongsTo('user');
// check if the user relationship is loaded
var isLoaded = userRef.value() !== null;
// get the record of the reference (null if not yet available)
var user = userRef.value();
// get the identifier of the reference
if (userRef.remoteType() === "id") {
var id = userRef.id();
} else if (userRef.remoteType() === "link") {
var link = userRef.link();
}
// load user (via store.findRecord or store.findBelongsTo)
userRef.load().then(...)
// or trigger a reload
userRef.reload().then(...)
// provide data for reference
userRef.push({
type: 'user',
id: 1,
attributes: {
username: "@user"
}
}).then(function(user) {
userRef.value() === user;
});
```
@method belongsTo
@param {String} name of the relationship
@since 2.5.0
@return {BelongsToReference} reference for this relationship
*/
belongsTo: function(name) {
return this._internalModel.referenceFor('belongsTo', name);
},

hasMany() {
assert("The `hasMany` method is not available on DS.Model, a DS.Snapshot was probably expected. Are you passing a DS.Model instead of a DS.Snapshot to your serializer?", false);
/**
Get the reference for the specified hasMany relationship.
Example
```javascript
// models/blog.js
export default DS.Model.extend({
comments: DS.hasMany({ async: true })
});
var blog = store.push({
type: 'blog',
id: 1,
relationships: {
comments: {
data: [
{ type: 'comment', id: 1 },
{ type: 'comment', id: 2 }
]
}
}
});
var commentsRef = blog.hasMany('comments');
// check if the comments are loaded already
var isLoaded = commentsRef.value() !== null;
// get the records of the reference (null if not yet available)
var comments = commentsRef.value();
// get the identifier of the reference
if (commentsRef.remoteType() === "ids") {
var ids = commentsRef.ids();
} else if (commentsRef.remoteType() === "link") {
var link = commentsRef.link();
}
// load comments (via store.findMany or store.findHasMany)
commentsRef.load().then(...)
// or trigger a reload
commentsRef.reload().then(...)
// provide data for reference
commentsRef.push([{ type: 'comment', id: 1 }, { type: 'comment', id: 2 }]).then(function(comments) {
commentsRef.value() === comments;
});
```
@method hasMany
@param {String} name of the relationship
@since 2.5.0
@return {HasManyReference} reference for this relationship
*/
hasMany: function(name) {
return this._internalModel.referenceFor('hasMany', name);
},

setId: Ember.observer('id', function () {
Expand Down Expand Up @@ -872,130 +982,6 @@ if (Ember.setOwner) {
});
}

if (isEnabled("ds-references")) {

Model.reopen({

/**
Get the reference for the specified belongsTo relationship.
Example
```javascript
// models/blog.js
export default DS.Model.extend({
user: DS.belongsTo({ async: true })
});
var blog = store.push({
type: 'blog',
id: 1,
relationships: {
user: { type: 'user', id: 1 }
}
});
var userRef = blog.belongsTo('user');
// check if the user relationship is loaded
var isLoaded = userRef.value() !== null;
// get the record of the reference (null if not yet available)
var user = userRef.value();
// get the identifier of the reference
if (userRef.remoteType() === "id") {
var id = userRef.id();
} else if (userRef.remoteType() === "link") {
var link = userRef.link();
}
// load user (via store.findRecord or store.findBelongsTo)
userRef.load().then(...)
// or trigger a reload
userRef.reload().then(...)
// provide data for reference
userRef.push({
type: 'user',
id: 1,
attributes: {
username: "@user"
}
}).then(function(user) {
userRef.value() === user;
});
```
@method belongsTo
@param {String} name of the relationship
@return {BelongsToReference} reference for this relationship
*/
belongsTo: function(name) {
return this._internalModel.referenceFor('belongsTo', name);
},

/**
Get the reference for the specified hasMany relationship.
Example
```javascript
// models/blog.js
export default DS.Model.extend({
comments: DS.hasMany({ async: true })
});
var blog = store.push({
type: 'blog',
id: 1,
relationships: {
comments: {
data: [
{ type: 'comment', id: 1 },
{ type: 'comment', id: 2 }
]
}
}
});
var commentsRef = blog.hasMany('comments');
// check if the comments are loaded already
var isLoaded = commentsRef.value() !== null;
// get the records of the reference (null if not yet available)
var comments = commentsRef.value();
// get the identifier of the reference
if (commentsRef.remoteType() === "ids") {
var ids = commentsRef.ids();
} else if (commentsRef.remoteType() === "link") {
var link = commentsRef.link();
}
// load comments (via store.findMany or store.findHasMany)
commentsRef.load().then(...)
// or trigger a reload
commentsRef.reload().then(...)
// provide data for reference
commentsRef.push([{ type: 'comment', id: 1 }, { type: 'comment', id: 2 }]).then(function(comments) {
commentsRef.value() === comments;
});
```
@method hasMany
@param {String} name of the relationship
@return {HasManyReference} reference for this relationship
*/
hasMany: function(name) {
return this._internalModel.referenceFor('hasMany', name);
}
});

}

Model.reopenClass(RelationshipsClassMethodsMixin);
Model.reopenClass(AttrClassMethodsMixin);

Expand Down
Loading

0 comments on commit f699952

Please sign in to comment.