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

allowing to pass an has many relationship on create record. Fixes #1497 #2400

Merged
merged 1 commit into from May 18, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 13 additions & 4 deletions packages/ember-data/lib/system/relationships/has-many.js
Expand Up @@ -2,6 +2,7 @@
@module ember-data
*/

import computedPolyfill from "ember-data/utils/computed-polyfill";
import Model from "ember-data/system/model";
import normalizeModelName from "ember-data/system/normalize-model-name";

Expand Down Expand Up @@ -120,10 +121,18 @@ function hasMany(type, options) {
key: null
};

return Ember.computed(function(key) {
var relationship = this._relationships[key];
return relationship.getRecords();
}).meta(meta).readOnly();
return computedPolyfill({
get: function(key) {
var relationship = this._relationships[key];
return relationship.getRecords();
},
set: function(key, records) {
var relationship = this._relationships[key];
relationship.clear();
relationship.addRecords(records);
return relationship.getRecords();
}
}).meta(meta);
}

Model.reopen({
Expand Down
Expand Up @@ -369,8 +369,7 @@ test("create - a serializer's attribute mapping takes precedence over keyForRela

run(function() {
var comment = store.createRecord('comment', { id: "a-comment-id", name: "First!" });
var post = store.createRecord('post', { id: "some-uuid", name: "The Parley Letter" });
post.get('comments').pushObject(comment);
var post = store.createRecord('post', { id: "some-uuid", name: "The Parley Letter", comments: [comment] });

post.save().then(async(function(post) {
deepEqual(passedHash.data, { post: { opinions: ["a-comment-id"], id: "some-uuid", name: "The Parley Letter" } });
Expand Down
Expand Up @@ -892,19 +892,19 @@ test("When a record is created on the client, its async hasMany arrays should be
});
});

test("a records SYNC HM relationship property is readOnly", function() {
test("we can set records SYNC HM relationship", function() {
expect(1);
var post = run(function() {
return env.store.createRecord('post');
});

raises(function() {
post.set('comments');
}, 'Cannot Set: comments on: ' + Ember.inspect(post));
run(function() {
post.set('comments', env.store.pushMany('comment', [{ id: 1, body: "First" }, { id: 2, body: "Second" }]));
});
equal(get(post, 'comments.length'), 2, "we can set HM relationship");
});


test("a records ASYNC HM relationship property is readOnly", function() {
test("We can set records ASYNC HM relationship", function() {
expect(1);
Post.reopen({
comments: DS.hasMany('comment', { async: true })
Expand All @@ -913,10 +913,13 @@ test("a records ASYNC HM relationship property is readOnly", function() {
var post = run(function() {
return env.store.createRecord('post');
});
run(function() {
post.set('comments', env.store.pushMany('comment', [{ id: 1, body: "First" }, { id: 2, body: "Second" }]));
});

raises(function() {
run(post, 'set', 'comments');
}, 'Cannot Set: comments on: ' + Ember.inspect(post));
post.get('comments').then(async(function(comments) {
equal(comments.get('length') , 2, "we can set async HM relationship");
}));
});

test("When a record is saved, its unsaved hasMany records should be kept", function () {
Expand Down
26 changes: 24 additions & 2 deletions packages/ember-data/tests/unit/store/create-record-test.js
@@ -1,13 +1,23 @@
var store, container, Record;
var store, container, Record, Storage;
var run = Ember.run;

module("unit/store/createRecord - Store creating records", {
setup: function() {
store = createStore({ adapter: DS.Adapter.extend() });

Record = DS.Model.extend({
title: DS.attr('string')
});

Storage = DS.Model.extend({
name: DS.attr('name'),
records: DS.hasMany('record')
});

store = createStore({
adapter: DS.Adapter.extend(),
record: Record,
storage: Storage
});
}
});

Expand All @@ -21,6 +31,18 @@ test("doesn't modify passed in properties hash", function() {
deepEqual(attributes, { foo: 'bar' }, "The properties hash is not modified");
});

test("allow passing relationships as well as attributes", function() {
var records, storage;
run(function() {
records = store.pushMany(Record, [{ id: 1, title: "it's a beautiful day" }, { id: 2, title: "it's a beautiful day" }]);
storage = store.createRecord(Storage, { name: 'Great store', records: records });
});

equal(storage.get('name'), 'Great store', "The attribute is well defined");
equal(storage.get('records').findBy('id', '1'), Ember.A(records).findBy('id', '1'), "Defined relationships are allowed in createRecord");
equal(storage.get('records').findBy('id', '2'), Ember.A(records).findBy('id', '2'), "Defined relationships are allowed in createRecord");
});

module("unit/store/createRecord - Store with models by dash", {
setup: function() {
var env = setupStore({
Expand Down