/** * @required $ jQuery */ (function (ns) { // part of jquery function fnProxy(fn, context) { if ( typeof context === "string" ) { var tmp = fn[ context ]; context = fn; fn = tmp; } // Quick check to determine if target is callable, in the spec // this throws a TypeError, but we will just return undefined. if ( !jQuery.isFunction( fn ) ) { return undefined; } // Simulated bind var args = Array.prototype.slice.call( arguments, 2 ), proxy = function() { return fn.apply( context, args.concat( Array.prototype.slice.call( arguments ) ) ); }; // Set the guid of unique handler to the same of original handler, so it can be removed proxy.guid = fn.guid = fn.guid || proxy.guid || jQuery.guid++; return proxy; } /** * @alias app.report.contact.admin.Form * @param {Object} options * @constructor */ ns.Form = function (options) { this.construct.apply(this, arguments); }; var fn = ns.Form.prototype; fn.construct = function (options) { this.options = $.extend({ requestUrl: undefined, ids: { form: undefined, id: undefined, userId: undefined } }, options); this.element = {}; this.element.form = $('#' + this.options.ids.form); this.element.id = $('#' + this.options.ids.id, this.element.form); this.element.userId = $('#' + this.options.ids.userId, this.element.form); this.element.id.bind('change.contactForm', fnProxy(this.onIdChange, this)); }; fn.onIdChange = function () { var id = this.element.id.val(); this.element.userId.attr('disabled', true).addClass('disabled').empty(); if (null == id || '' == id) { return; } var request = { action: 'loadUsers', id: id }; $.ajax({ url: this.options.requestUrl || window.location.href, data: request, type: 'post', dataType: 'json', success: fnProxy(this.onUserLoadDone, this), error: fnProxy(this.onUserLoadFail, this) }); }; fn.onUserLoadDone = function (response) { var list = [], i, c; if (response.userList && response.userList.length) { for (i = 0, c = response.userList.length; i < c; ++i) { list.push({ value: response.userList[i].id, title: response.userList[i].name }); } } this.updateElementUserId(list); }; fn.onUserLoadFail = function (response) { this.updateElementUserId(null); }; fn.updateElementUserId = function (list) { if (null == list) { this.element.userId.attr('disabled', true).addClass('disabled').empty(); return; } qs.form.select.setOptions(this.element.userId, list, 'Select User'); this.element.userId.attr('disabled', false).removeClass('disabled'); }; })(qs.defineNS('app.report.contact.admin'));