Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add flag for including associations in toJSON()

  • Loading branch information...
commit 0b7fabb6538d076472b1b3d7c9e5b825bba19b92 1 parent 2bb8d55
tomhuda authored
7 packages/ember-data/lib/system/model/associations.js
@@ -4,7 +4,8 @@ require("ember-data/system/model/model");
4 4
5 5 DS.Model.reopenClass({
6 6 typeForAssociation: function(name) {
7   - return Ember.get(this, 'associationsByName').get(name);
  7 + var association = get(this, 'associationsByName').get(name);
  8 + return association && association.type;
8 9 },
9 10
10 11 associations: Ember.computed(function() {
@@ -17,6 +18,7 @@ DS.Model.reopenClass({
17 18
18 19 if (typeof type === 'string') {
19 20 type = getPath(this, type, false) || getPath(window, type);
  21 + meta.type = type;
20 22 }
21 23
22 24 if (!typeList) {
@@ -40,9 +42,10 @@ DS.Model.reopenClass({
40 42
41 43 if (typeof type === 'string') {
42 44 type = getPath(this, type, false) || getPath(window, type);
  45 + meta.type = type;
43 46 }
44 47
45   - map.set(name, type);
  48 + map.set(name, meta);
46 49 }
47 50 });
48 51
27 packages/ember-data/lib/system/model/model.js
@@ -122,6 +122,7 @@ DS.Model = Ember.Object.extend({
122 122 attributes = get(type, 'attributes'),
123 123 primaryKey = get(this, 'primaryKey'),
124 124 id = get(this, 'id'),
  125 + store = get(this, 'store'),
125 126 associations;
126 127
127 128 options = options || {};
@@ -144,12 +145,28 @@ DS.Model = Ember.Object.extend({
144 145 associations = get(type, 'associationsByName');
145 146
146 147 associations.forEach(function(key, meta) {
147   - var association = get(this, key);
148   -
149   - if (meta.kind === 'hasMany') {
150   - var ids = association;
  148 + if (options.associations && meta.kind === 'hasMany') {
  149 + var association = get(this, key),
  150 + type = meta.type,
  151 + typeMap = store.typeMapFor(type),
  152 + clientIdToIdMap = typeMap.cidToId,
  153 + clientIds = get(association, 'content'),
  154 + ids = [],
  155 + clientId, id;
  156 +
  157 + for (var i=0, l=clientIds.length; i<l; i++) {
  158 + clientId = clientIds[i];
  159 +
  160 + id = clientIdToIdMap[clientId];
  161 +
  162 + if (id !== undefined) {
  163 + ids.push(id);
  164 + }
  165 + }
  166 +
  167 + result[key] = ids;
151 168 }
152   - });
  169 + }, this);
153 170
154 171 return result;
155 172 },
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
200 200 deepEqual(record.toJSON(), { firstName: "Yehuda", last_name: "Katz", country: null, isHipster: false }, "the data is extracted by attribute");
201 201 });
202 202
  203 +test("toJSON returns a hash containing the JSON representation of the record", function() {
  204 + var PhoneNumber = DS.Model.extend({
  205 + number: DS.attr('string')
  206 + });
  207 +
  208 + var Contact = DS.Model.extend({
  209 + name: DS.attr('string'),
  210 + phoneNumbers: DS.hasMany(PhoneNumber)
  211 + });
  212 +
  213 + store.load(PhoneNumber, { id: 7, number: '123' });
  214 + store.load(PhoneNumber, { id: 8, number: '345' });
  215 +
  216 + store.load(Contact, { id: 1, name: "Chad", phoneNumbers: [7, 8] });
  217 +
  218 + var record = store.find(Contact, 1);
  219 +
  220 + deepEqual(record.toJSON(), { id: 1, name: "Chad" }, "precond - associations not included by default");
  221 + deepEqual(record.toJSON({ associations: true }),
  222 + { id: 1, name: "Chad", phoneNumbers: [7,8] },
  223 + "associations are included when association flag is set");
  224 +
  225 + store.load(PhoneNumber, { id: 9, number: '789' });
  226 + var phoneNumber = store.find(PhoneNumber, 9);
  227 +
  228 + record.get('phoneNumbers').pushObject(phoneNumber);
  229 +
  230 + deepEqual(record.toJSON({ associations: true }),
  231 + { id: 1, name: "Chad", phoneNumbers: [7,8,9] },
  232 + "association is updated after editing associations array");
  233 +});
  234 +
203 235 var Person, store, array;
204 236
205 237 module("DS.Model updating", {

0 comments on commit 0b7fabb

Please sign in to comment.
Something went wrong with that request. Please try again.