Ember-data: find, findQuery etc. and hasMany associations


#1

Hey fellow Embereños,

First of all, a little background: I’ve been using ember-data for months. Back when I started using the library, I had to implement a few workarounds in order to make it properly handle associations when issuing calls to App.store.findQuery, App.store.load etc. to make sure children were added to their parents correctly.

Now, months later, I’m running bleeding-edge, daily-built ember-data revision 12. I’m using the DS.RESTAdapter out-of-the-box. I’ve changed nothing, no workarounds have been added, it’s all vanilla ember-data.

Casually inspecting things, when finding a record, didFindRecord chimes in happily. Creating records works like a charm, deleting them too etc. All the basics are working fine - hooray!

Well, except this: 1) either I’m missing something truly fundamental or 2) associations are still not handled properly.

Further explanation:

Say I have an App.User and an App.Friend. A user has friends, nice! Here are the two models:

App.User = DS.Model.extend({

	firstname: DS.attr('string'),
	lastname: DS.attr('string'),
	friends: DS.hasMany('App.Friend')

});

App.Friend = DS.Model.extend({

	is_classmate: DS.attr('boolean'),
	user: DS.belongsTo('App.User')

});

Now, in five simple steps (four, really), this is what I attempt that does not work:

Step 1: finding a user:

var user = App.User.find('1');

This works nicely and a user is returned. Server-side, this user has no friends yet, so this is how the returned JSON looks:

{
    "user": {
        "firstname": "Kasper",
        "id": "1",
        "lastname": "Tidemann",
        "friends": []
    }
} 

Step 2: finding a user’s friends:

user.get('friends.content').length;

This is just for simple testing. I would never get the content directly in practice, but anyhow, this returns a flat 0. No App.Friends are loaded. So far, this is correct.

Step 3: later on, another find is issued:

var friend = App.Friend.find('2');

Sometime later on, the client got word of a new friend having been created and added to the user in question. This might have happened on another device.

Therefore, in order to find the newly added friend, the above find call is issued. Finding the App.Friend works, of course, and this is the JSON:

{
    "friend": {
        "is_classmate": true,
        "id": "2",
        "user_id": "1"
    }
} 

Step 4: getting the user’s firstname via the friend:

friend.get('user.firstname');

Yup, this does indeed return "Kasper". In other words: the newly found App.Friend knows it belongs to the user in question.

Step 5: finding a user’s friends (again):

user.get('friends.content').length;

… This still returns 0. The user still has no friends, apparently. :frowning:

Reprise:

So, put shortly, this is what I do:

1) var user = App.User.find('1'); -> Works.

2) user.get('friends.content').length; -> Returns 0.

3) var friend = App.Friend.find('2'); -> Works.

4) user.get('friends.content').length; -> Still returns 0.

If I fire off an App.Friend.createRecord({ user: user }); manually and then call user.get('friends.content').length;, it will indeed return 1 as expected. So that works… But not the finding part. The same goes for findQuery and findMany.

Months ago, part of my code would make sure to add children to their parents upon the triggering of their respective didLoad events. This wasn’t pretty but worked. From the above, it seems I still need to do that?

Example code:

For a tiny, practical example, please refer to http://kaspertidemann.com/temp/ember-data-test/

Please, if anybody has any input on this, it’s greatly appreciated. If anything, just let me know if this behavior is intentional and will never change, in which case I will adjust my code accordingly.

Thank you on beforehand,

Kasper.


Update parent model when children is updated
#2

The issue still exists in the latest version of ember-data, can I know what did you do to solve this issue?