Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: xdissent/data
base: 1b606c29fb
...
head fork: xdissent/data
compare: ac3898d89d
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 2 contributors
Commits on Apr 18, 2012
@kyptin kyptin Fix computed props for embedded hasMany (#191):
This commit fixes an issue where:
- a model A `hasMany` model B's,
- the model B objects are embedded in a model A object, and
- model A has a computed property that depends on *a property of* the
  hasMany association.

In such a case, what should have been the IDs of the embedded model B
objects were actually the objects themselves, which was causing
problems.

This bug is fixed now and covered by a new test.
f81d11c
Commits on May 01, 2012
@xdissent Merge remote-tracking branch 'ky/master' into HEAD ac3898d
View
6 packages/ember-data/lib/system/model/model.js
@@ -292,6 +292,12 @@ DS.Model = Ember.Object.extend(Ember.Evented, {
if (cachedValue) {
var ids = data.get(name) || [];
+ if (association.options.embedded) {
+ var primaryKey = association.type.proto().primaryKey;
+ ids = Ember.ArrayUtils.map(ids, function(obj) {
+ return get(obj, primaryKey);
+ });
+ }
var clientIds = Ember.ArrayUtils.map(ids, function(id) {
return store.clientIdForId(association.type, id);
});
View
36 packages/ember-data/tests/unit/associations_test.js
@@ -261,6 +261,42 @@ test("embedded associations work the same as referenced ones, and have the same
equal(getPath(kselden, 'tags.length'), 0, "if no association is provided, an empty list is returned");
});
+test("embedded hasMany associations work with computed properties", function() {
+ var Tag = DS.Model.extend({
+ // Use a non-default primaryKey to ensure that the primaryKey setting is
+ // honored.
+ primaryKey: '_id_',
+ name: DS.attr('string')
+ });
+
+ var Person = DS.Model.extend({
+ name: DS.attr('string'),
+ tags: DS.hasMany(Tag, { embedded: true }),
+ numTags: Ember.computed(function() {
+ return this.getPath('tags.length');
+ }).property('tags.length').cacheable()
+ });
+
+ var store = DS.Store.create();
+ store.load(Person, {
+ id: 1, name: "Tom Dale", tags: [
+ { _id_: 5, name: "friendly" },
+ { _id_: 2, name: "smarmy" }
+ ]
+ });
+
+ // This test addresses Issue #191. Without the fix, the following line will
+ // generate this error: "Adapter is either null or does not implement
+ // `findMany` method".
+ var person = store.find(Person, 1);
+ strictEqual(get(person, 'name'), "Tom Dale",
+ "precond - retrieves person record from store");
+ strictEqual(getPath(person, 'tags.length'), 2,
+ "the list of tags should have the correct length");
+ strictEqual(get(person, 'numTags'), 2,
+ "the computed property should give the right value");
+});
+
test("it is possible to add a new item to an association", function() {
var Tag = DS.Model.extend({
name: DS.attr('string')

No commit comments for this range

Something went wrong with that request. Please try again.