var LinkedSelect = function() { this.initialize.apply(this, arguments) }; LinkedSelect.prototype = { initialize: function(master, slave, options) { this.master = (typeof master == 'string') ? _$(master).el[0] : master; this.slave = (typeof slave == 'string') ? _$(slave).el[0] : slave; this.options = options || {}; this.actionURL = this.options.url; var self = this; YAHOO.util.Event.addListener(this.master, 'change', function() { self.updateSlave(); }); if (this.options.updateNow) this.updateSlave(); }, updateSlave: function() { if (!this.actionURL) throw new Error('No update URL specified for LinkedSelect'); var list = this.master, data = {}; data[list.name] = list.value; if (this.options.alsoPasses) this.options.alsoPasses.forEach(function(field) { data[field.name] = field.value; }); var query = ''; for (var key in data) query += (query ? '&' : '') + encodeURIComponent(key) + '=' + encodeURIComponent(data[key]); YAHOO.util.Connect.asyncRequest('GET', this.actionURL + '?' + query, { scope: this, success: function(transport) { var value = this.slave.value; updateSelect(this.slave, transport.responseText); var option = this.slave.options[0]; for (var i = 0, n = this.slave.options.length; i < n; i++) { if (this.slave.options[i].value == value) option = this.slave.options[i]; } if (option) option.selected = true; if (this.options.afterUpdate) this.options.afterUpdate(); } }); } }; /** * Changes the innerHTML of a drop-down menu. * See http://code.google.com/p/ojay/source/browse/tags/0.1.2/source/core/dom_insertion.js * @param {HTMLElement} select * @param {String} html */ function updateSelect(select, html) { var div = document.createElement('div'); div.innerHTML = ''; select.innerHTML = ''; Array.from(div.firstChild.childNodes).forEach(function(node) { select.appendChild(node); }); };