31e2b397e15442a2d3ba42db7bd4584e

I have the following code that works. However, it is the same pattern over and over. How do I refactor this into a loop?

if ($('invoice_discount')) {
  	    this.invoice_discount = this.form.down('#invoice_discount');
		    this.invoice_discount.observe('change', this._checkFormat.bindAsEventListener(this));
  	  }
  	  if ($('invoice_vat')) {
  	    this.invoice_vat = this.form.down('#invoice_vat');
		    this.invoice_vat.observe('change', this._checkFormat.bindAsEventListener(this));
  	  }
  	  if ($('invoice_salestax')) {
  	    this.invoice_salestax = this.form.down('#invoice_salestax');
		    this.invoice_salestax.observe('change', this._checkFormat.bindAsEventListener(this));
  	  }
  	  if ($('invoice_freight')) {
  	    this.invoice_freight = this.form.down('#invoice_freight');
		    this.invoice_freight.observe('change', this._checkFormat.bindAsEventListener(this));
  	  }

Refactorings

No refactoring yet !

Aacfa176a8d73ca75b90b6375151765a

paul.wilkins.myopenid.com

November 1, 2009, November 01, 2009 19:54, permalink

2 ratings. Login to rate!

Edit: using a normal for loop, as with for...in on an array there's no guarantee that other properties aren't prototyped in addition

var fields = ['invoice_discount', 'invoice_vat', 'invoice_salestax', 'invoice_freight'],
    i,
    field;
for (i = 0; i < fields.length; i++) {
    field = fields[i];
    this[field] = this.form.down('#' + field);
    this[field].observe('change', this._checkFormat.bindAsEventListener(this));
}
F1e3ab214a976a39cfd713bc93deb10f

Tj Holowaychuk

November 1, 2009, November 01, 2009 20:56, permalink

2 ratings. Login to rate!

dont use for / in with an array

B95506b4b4d1f117cf634ee6c53a2d6b

James

November 4, 2009, November 04, 2009 21:33, permalink

2 ratings. Login to rate!

Agreed with TJ

$A(['discount', 'vat', 'salestax', 'freight']).each(function(id) {
  this['invoice_' + id] = this.form.down('#invoice_' + id);
  this['invoice_' + id].observe('change', this._checkFormat.bindAsEventListener(this));
}.bind(this));
34db50b7ce2e115afadf5a765b950739

Thomas Salvador

November 15, 2009, November 15, 2009 11:10, permalink

No rating. Login to rate!

hi.
i think the versions of paul and james should be combined.
removed the redundant field name calculation to get it down from three concatenations to two, per field. (compiler might optimize this on its own.)

$A(['discount', 'vat', 'salestax', 'freight']).each(function(id) {
  fullfield = 'invoice_' + id;
  this[fullfield] = this.form.down('#' + fullfield);
  this[fullfield].observe('change', this._checkFormat.bindAsEventListener(this));
}.bind(this));
13381166d87f50a05d5f9ee1077adbb2

Paul

November 18, 2009, November 18, 2009 20:16, permalink

No rating. Login to rate!

Does this work?

this.form.select('#invoice_discount, #invoice_vat, #invoice_salestax, #invoice_freight').invoke('observe', 'change', this._checkFormat.bindAsEventListener(this));

Your refactoring





Format Copy from initial code

or Cancel