emberjs/data — Issue #152: Parent -> Child -> Parent association binding chains should not cause an infinite loop.
Author: ghempton
Created: 2012-03-09 01:37:15+00:00
State: open

Below is a test case:

```javascript
test("Parent -> Child -> Parent association binding chains should not cause an infinite loop.", function() {
expect(1);

var Bill = DS.Model.extend();
var Item = DS.Model.extend();

var adapter = DS.Adapter.create({
find: function(store, type, id) {
debugger;
equals(type, Bill, 'Should not load anything but Bill');
store.load(Bill, {id: 1, tax: 0.8, items: [{cost: 1, bill_id: 1}]});
}
});
var store = DS.Store.create({
isDefaultStore: true,
adapter: adapter,
});

Bill.reopen({
tax: DS.attr('number'),
items: DS.hasMany(Item, {embedded: true}),
totalCost: Ember.computed(function() {
return 0; // doesn't matter
}).property('items.@each.costWithTax')
});

Item.reopen({
cost: DS.attr('number'),
bill: DS.belongsTo(Bill),
costWithTax: Ember.computed(function() {
var tax = this.getPath('bill.tax') || 0;
return this.get('cost') * tax;
}).property('cost', 'bill.tax')
});

var Container = DS.Model.extend({
bill: DS.hasOne(Bill, {embedded: true})
});

store.load(Container, {id: 1, bill: {id: 1, tax: 0.8, items: [{id: 1, cost: 1, bill_id: 1}]}});
var container = store.find(Container, 1);

Ember.addObserver(container, 'bill.totalCost', function() {});

ok(true, 'should get here successfully');

});
```

This is a contrived example, but I encountered this in the wild in a more complex app. This *should* work without entering an infinite loop, even though the logic could be rewritten in a better fashion.