I've defined two resources - Foo and Bar - where Foo contains one Bar. However, when inserting some basic data in this format, if multiple Foo's reference the same Bar, then only the most recently loaded Foo is given a reference to it. Previous Foo's have no Bar (it is undefined.)
The resources are defined like so:
DS.defineResource({
name: 'Foo',
relations: {
hasOne: {
Bar: {
foreignKey: 'fooId',
localField: 'bar'
}
}
}
});
DS.defineResource({
name: 'Bar',
relations: {
belongsTo: {
Foo: {
localField: "foo",
localKey: "fooId"
}
}
}
});
And I am injecting the following test data:
var foosJson = [
{
id: 1,
bar: {
id: 100
}
}, {
id: 2,
bar: {
id: 100
}
}, {
id: 3,
bar: {
id: 200
}
}
];
Why does the first Foo not have a reference to the same Bar as the second Foo?
Fiddle with Jasmine tests here: http://jsfiddle.net/dimmreaper/c0o1kqd0/
It's not working because it looks like you're actually trying for a many-to-many relationship. "bar belongsTo foo" means that any individual bar belongs to (at most) exactly one foo, but here you're trying to make bar #100 belong to both foo #1 and foo #2, which isn't a belongsTo relationship.
Since each bar doesn't actually have the field that
localKey
expects (fooId
), JSData is trying to infer the field for you, and due to how the iteration works, JSData infers that bar #100 belongsTo foo #2, sofoo1.bar
is going to beundefined
.