Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add flag for including associations in toJSON()

  • Loading branch information...
commit 0b7fabb6538d076472b1b3d7c9e5b825bba19b92 1 parent 2bb8d55
tomhuda authored
View
7 packages/ember-data/lib/system/model/associations.js
@@ -4,7 +4,8 @@ require("ember-data/system/model/model");
DS.Model.reopenClass({
typeForAssociation: function(name) {
- return Ember.get(this, 'associationsByName').get(name);
+ var association = get(this, 'associationsByName').get(name);
+ return association && association.type;
},
associations: Ember.computed(function() {
@@ -17,6 +18,7 @@ DS.Model.reopenClass({
if (typeof type === 'string') {
type = getPath(this, type, false) || getPath(window, type);
+ meta.type = type;
}
if (!typeList) {
@@ -40,9 +42,10 @@ DS.Model.reopenClass({
if (typeof type === 'string') {
type = getPath(this, type, false) || getPath(window, type);
+ meta.type = type;
}
- map.set(name, type);
+ map.set(name, meta);
}
});
View
27 packages/ember-data/lib/system/model/model.js
@@ -122,6 +122,7 @@ DS.Model = Ember.Object.extend({
attributes = get(type, 'attributes'),
primaryKey = get(this, 'primaryKey'),
id = get(this, 'id'),
+ store = get(this, 'store'),
associations;
options = options || {};
@@ -144,12 +145,28 @@ DS.Model = Ember.Object.extend({
associations = get(type, 'associationsByName');
associations.forEach(function(key, meta) {
- var association = get(this, key);
-
- if (meta.kind === 'hasMany') {
- var ids = association;
+ if (options.associations && meta.kind === 'hasMany') {
+ var association = get(this, key),
+ type = meta.type,
+ typeMap = store.typeMapFor(type),
+ clientIdToIdMap = typeMap.cidToId,
+ clientIds = get(association, 'content'),
+ ids = [],
+ clientId, id;
+
+ for (var i=0, l=clientIds.length; i<l; i++) {
+ clientId = clientIds[i];
+
+ id = clientIdToIdMap[clientId];
+
+ if (id !== undefined) {
+ ids.push(id);
+ }
+ }
+
+ result[key] = ids;
}
- });
+ }, this);
return result;
},
View
32 packages/ember-data/tests/unit/model_test.js
@@ -200,6 +200,38 @@ test("toJSON returns a hash containing the JSON representation of the record", f
deepEqual(record.toJSON(), { firstName: "Yehuda", last_name: "Katz", country: null, isHipster: false }, "the data is extracted by attribute");
});
+test("toJSON returns a hash containing the JSON representation of the record", function() {
+ var PhoneNumber = DS.Model.extend({
+ number: DS.attr('string')
+ });
+
+ var Contact = DS.Model.extend({
+ name: DS.attr('string'),
+ phoneNumbers: DS.hasMany(PhoneNumber)
+ });
+
+ store.load(PhoneNumber, { id: 7, number: '123' });
+ store.load(PhoneNumber, { id: 8, number: '345' });
+
+ store.load(Contact, { id: 1, name: "Chad", phoneNumbers: [7, 8] });
+
+ var record = store.find(Contact, 1);
+
+ deepEqual(record.toJSON(), { id: 1, name: "Chad" }, "precond - associations not included by default");
+ deepEqual(record.toJSON({ associations: true }),
+ { id: 1, name: "Chad", phoneNumbers: [7,8] },
+ "associations are included when association flag is set");
+
+ store.load(PhoneNumber, { id: 9, number: '789' });
+ var phoneNumber = store.find(PhoneNumber, 9);
+
+ record.get('phoneNumbers').pushObject(phoneNumber);
+
+ deepEqual(record.toJSON({ associations: true }),
+ { id: 1, name: "Chad", phoneNumbers: [7,8,9] },
+ "association is updated after editing associations array");
+});
+
var Person, store, array;
module("DS.Model updating", {
Please sign in to comment.
Something went wrong with that request. Please try again.