Skip to content

Commit

Permalink
Merge pull request #2400 from sly7-7/has-many-on-create-record
Browse files Browse the repository at this point in the history
allowing to pass an has many relationship on create record. Fixes #1497
  • Loading branch information
igorT committed May 18, 2015
2 parents dea143d + e9ddabd commit 2f83303
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
17 changes: 13 additions & 4 deletions packages/ember-data/lib/system/relationships/has-many.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2f83303

Please sign in to comment.