-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Inject relations for parent resource on create #132
Comments
Yes, I've thought about this. I'll need to augment the injection to search the store for existing relations to update the references. |
This doesn't seem to be fixed, see plunker: http://plnkr.co/edit/KjTHJy?p=preview |
Actually, your plunkr demonstrates a capability that hasn't been implemented yet. Issue #132 establishes belongsTo relations of the items being injected. In your plunkr there is I think the feature you're now requesting is "establish hasMany relations of the items being injected", which belongs in a new issue.
|
Ah, alright. I think its my fault for being unclear in my original description of this issue. I stated that "the parent resource doesn't seem to get updated with the relation." and with this I meant that the parent object of the created item should get updated with a relation to the inserted object. The code example may also be a bit unclear as it doesn't state what the desired result would be. The comment should read: DS.create('user', { name: 'test', organizationId: 1}).then(function() {
DS.get('organization', 1); // This should contain a relation to the newly created user
}); And as you stated, this should now be put in its own issue. Should I give it another try and rephrase the issue differently or do you have a good formulation for it? |
Hah, I misinterpreted! I've just about got it fixed. Don't worry about it.
|
I decided to forgo a new issue. Should be fixed now.
|
Alright it seems like I can't formulate the problem in an understandable way, sorry for that. What the current solution seems to do is update any hasMany relationship for the newly injected item right? // Current solution
// Note: Creating an _organization_
DS.create('organization', {id:1, name: 'orgname'}, {findHasMany: true}).then(
function() {
var org1 = DS.get('organization', 1);
// org1 will now have relations to any related users
}
); The original issue tries to specify a problem where other objects should get a relation to the newly inserted object. // Wanted solution
// Note: Creating a _user_
DS.create('user', {id:1,name: 'test',organizationId: 1}).then(function() {
// Get the _organization_
var org1 = DS.get('organization', 1);
// org1 should contain a relation to the newly created user like this:
{
id:1,
name:"orgname",
users: [ { id:1, name: "test", organizationId: 1 }]
}
}); And by the way, doesn't these lines in loadRelations.js set the default values to true? if (!('findBelongsTo' in options)) {
options.findBelongsTo = true;
}
if (!('findHasMany' in options)) {
options.findHasMany = true;
} |
Maybe the solution could be a synchronous method for re-loading local relations? Like this: DS.create('users', { name: 'test', organizationId: 1}).then(function() {
var org1 = DS.get('organization', 1);
DS.reloadRelations('organization', org1, ['user']);
console.log(org1.users); // This will now contain the new user
}); |
This was removed in the last commit: if (!('findBelongsTo' in options)) {
options.findBelongsTo = true;
}
if (!('findHasMany' in options)) {
options.findHasMany = true;
} Okay, now I think I understand what you're getting at. Everything I've done so far has been to update the newly injected items with references to any of their relations that are already in the store. You want everything that is already in the store to get updated with appropriate references to newly injected items. Right? |
Correcto! |
Okay, I'll come up with a solution. |
@Patrik-Lundqvist Take a look at bc0d5fa The following is what I implemented first: DS.inject('organization', { id: 1 }); // { id: 1 }
DS.inject('user', { id: 1, organizationId: 1 }, { findBelongsTo: true });
DS.get('user', 1); // { id: 1, organizationId: 1, organization: { id: 1 } } The following solves your original intention with #132: DS.inject('organization', { id: 1 }); // { id: 1 }
DS.inject('user', { id: 1, organizationId: 1 }, { linkInverse: true }); // { id: 1, organizationId: 1 }
DS.get('organization', 1); // { id: 1, users: [{ id: 1, organizationId: 1 }] } And they can be used together: DS.inject('organization', { id: 1 }); // { id: 1 }
DS.inject('user', { id: 1, organizationId: 1 }, { linkInverse: true, findBelongsTo: true });
DS.get('organization', 1); // { id: 1, users: [{ id: 1, organizationId: 1, organization: {...} }] }
DS.get('user', 1); // { id: 1, organizationId: 1, organization: { id: 1, users: [...] } } |
Works great, well done! |
When creating a new item that belongs to a another resource, the parent resource doesn't seem to get updated with the relation.
Example:
Creating item
The text was updated successfully, but these errors were encountered: