"interdependent" models


#1

My application needs to have “interdependent” models. The loot model for example, requires access to the percentageCut and bankBalance properties in the merchant model.

I believe I’m doing this wrong somehow. From what I understand, models should not (and cannot) access the properties of a controller.

How should I structure my ember application in this case? Thanks!

App.IndexController = Ember.ObjectController.extend({ 
needs: ['merchant','loot'] 
});

App.MerchantController = Ember.ObjectController.extend();

App.Merchant = Ember.Object.extend({
bankBalance: '0',
percentageCut: '10'
});

App.LootController = Ember.ArrayController.extend({
needs: ['merchant']
});

# The code for lootAmountAfterMerchantCut does not work, and is merely to provide an idea of what i require.

App.Loot = Ember.Object.extend({
name: null,
bankBalanceChanged: null,
previousBankBalanceChanged: null,
lootAmountAfterMerchantCut: function(){   
   return (bankBalanceChanged - merchant.bankBalance) * (100-merchant.percentageCut) /100
}.property('merchant.percentageCut','merchant.bankBalance)
});

#2

I’m not really sure what calculations you need to do, but it feels they don’t belong in the model. The Loot entity should only contains properties of the loot, and not of the merchant.

The calculations can be done in the controller, where you have access to the loot and merchant. If you need to do the calculations in multiple controllers, you could perhaps create a Calculation object that takes loot and merchants and returns the result of the calculation.

With correct binding, everything should stay in sync and not break the MVC paradigm.