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

[CLEANUP ds-references] #4347

Merged
merged 1 commit into from
Apr 28, 2016
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
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