Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Allow belongsTo associations without a corresponding hasMany #477

Closed
wants to merge 1 commit into from

4 participants

@pivotal-medici

No description provided.

@wagenet
Owner

This seems ok. @wycats, do we want to support this?

@tomdale
Owner

We feel like this brings adapter-level semantics into the application layer. We will try to fold this use case into the work we're doing this week in terms of allowing the adapter to influence the app layer in a way that produces understandable errors. Let's revisit this to see if it's fixed in a week or two.

@tomdale tomdale closed this
@sihingkk

And what conclusions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 14, 2012
  1. Allow belongsTo associations without a corresponding hasMany

    Ian Lesperance & Mark Gangl authored
This page is out of date. Refresh to see the latest.
View
8 packages/ember-data/lib/system/associations/one_to_many_change.js
@@ -254,12 +254,12 @@ DS.OneToManyChange.prototype = {
// If our lastParent clientId is not null, there will always be a
// materialized lastParent.
var lastParent = this.getLastParent();
- if (lastParent) {
+ if (lastParent && hasManyName) {
get(lastParent, hasManyName).removeObject(child);
}
// Don't do anything if the belongsTo is going from null back to null
- if (oldParent) {
+ if (oldParent && hasManyName) {
get(oldParent, hasManyName).addObject(child);
}
@@ -286,7 +286,7 @@ DS.OneToManyChange.prototype = {
// 1. The change happened from the belongsTo side
// 2. The record was moved to a new parent without explicitly
// removing it from the old parent first.
- if (oldParent) {
+ if (oldParent && hasManyName) {
get(oldParent, hasManyName).removeObject(child);
if (get(oldParent, 'isLoaded')) {
@@ -298,7 +298,7 @@ DS.OneToManyChange.prototype = {
// to ensure that the record is in its ManyArray. The `addObject`
// method only has an effect if the change happened from the
// belongsTo side.
- if (newParent) {
+ if (newParent && hasManyName) {
get(newParent, hasManyName).addObject(child);
if (get(newParent, 'isLoaded')) {
View
43 packages/ember-data/tests/integration/inverse_associations_test.js
@@ -61,3 +61,46 @@ test("When a record is added to a has-many association, the inverse belongsTo ca
equal(comment.get('redPost'), post, "redPost has been set on the comment");
equal(comment.get('bluePost'), null, "bluePost has not been set on the comment");
});
+
+test("setting a belongsTo without an inverse hasMany", function() {
+ Post = DS.Model.extend({
+ title: DS.attr('string')
+ });
+ Post.toString = function() { return "Post"; };
+
+ Comment = DS.Model.extend({
+ body: DS.attr('string'),
+ post: DS.belongsTo(Post)
+ });
+ Comment.toString = function() { return "Comment"; };
+
+ store.load(Post, { id: 1 });
+ store.load(Post, { id: 2 });
+ store.load(Comment, { id: 1, body: 'Pre-assigned comment', post: 1 });
+ store.load(Comment, { id: 2, body: 'Initially unassigned comment' });
+
+ var post1 = store.find(Post, 1),
+ post2 = store.find(Post, 1),
+ comment;
+
+ comment = store.find(Comment, 1);
+ equal(comment.get('post'), post1, "post is initially set to Post 1");
+ comment.set('post', null);
+ equal(comment.get('post'), null, "post has been unset");
+ comment.set('post', post1);
+ equal(comment.get('post'), post1, "post has been restored to Post 1");
+
+ comment = store.find(Comment, 1);
+ equal(comment.get('post'), post1, "post is initially set to Post 1");
+ comment.set('post', post2);
+ equal(comment.get('post'), post2, "post has been updated to Post 2");
+ comment.set('post', post1);
+ equal(comment.get('post'), post1, "post has been restored to Post 1");
+
+ comment = store.find(Comment, 2);
+ equal(comment.get('post'), null, "post is initially unset");
+ comment.set('post', post1);
+ equal(comment.get('post'), post1, "post has been set to Post 1");
+ comment.set('post', null);
+ equal(comment.get('post'), null, "post has been restored to null");
+});
Something went wrong with that request. Please try again.