On the app I am working on I display a two text fields - one with an amount in Euros and one with an amount in Pounds. I need the customer to be able to update either figure and for the other figure to automatically update.
The solution I have settled on looks like this:
Blah.HomeController = Ember.ObjectController.extend({
isInternalEuroUpdate: false,
exchangeRate: 1.2,
euros: '',
pounds: function(key, value) {
if (arguments.length === 2) {
this.set('isInternalEuroUpdate', true);
this.set('euros', value * this.get('exchangeRate'));
this.set('isInternalEuroUpdate', false);
}
return value;
}.property('euros'),
euroChange: function(value) {
if (!this.get('isInternalEuroUpdate')) {
this.set('pounds', this.get('euros') / this.get('exchangeRate'));
}
}.observes('euros')
});
(see it in action or edit it here: http://jsbin.com/AViZATE/103/ )
It seems pretty ugly to me (the isInternalEuroUpdate
variable smells bad) - is there a better way to deal with a situation like this?
Thanks!