0

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/

4

1 回答 1

1

它不起作用,因为看起来您实际上是在尝试建立多对多关系。“bar belongsTo foo”表示任何单个 bar (至多)恰好属于一个foo,但在这里你试图让 bar #100 同时属于 foo #1 和 foo #2,这不是 belongsTo 关系。

由于每个 bar 实际上没有localKey期望 ( fooId) 的字段,JSData 试图为您推断字段,并且由于迭代的工作方式,JSData 推断 bar #100 属于 foo #2,因此foo1.bar将是undefined

于 2016-02-10T17:19:20.313 回答