From 109b367fc31aa53e7022f11a04b4e1daddee9c90 Mon Sep 17 00:00:00 2001 From: Ashesh Vashi Date: Mon, 4 Jan 2016 11:34:40 +0530 Subject: [PATCH] Allow to specify the options as a function, which returns array in form of (label, value) tuple in the SelectControl. We will apply the transform function, while rendering the control, and not during intialization. This will allow us to generate different options data based on the dependent values. --- web/pgadmin/browser/static/js/node.ui.js | 11 ++--- web/pgadmin/static/js/backform.pgadmin.js | 51 +++++++++++++++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/web/pgadmin/browser/static/js/node.ui.js b/web/pgadmin/browser/static/js/node.ui.js index 560890705..79bbd8474 100644 --- a/web/pgadmin/browser/static/js/node.ui.js +++ b/web/pgadmin/browser/static/js/node.ui.js @@ -77,13 +77,10 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) { */ transform = this.field.get('transform') || self.defaults.transform; if (transform && _.isFunction(transform)) { - try { - data = transform.apply(self, [data]); - } catch(e) { - // Do nothing - data = [] - m.trigger('pgadmin-view:transform:error', m, self.field, e); - } + // We will transform the data later, when rendering. + // It will allow us to generate different data based on the + // dependencies. + self.field.set('options', transform.bind(self, data)); } self.field.set('options', data); } diff --git a/web/pgadmin/static/js/backform.pgadmin.js b/web/pgadmin/static/js/backform.pgadmin.js index 7b14e8d47..ba5b6693c 100644 --- a/web/pgadmin/static/js/backform.pgadmin.js +++ b/web/pgadmin/static/js/backform.pgadmin.js @@ -156,6 +156,57 @@ }); }; + /* + * Overriding the render function of the select control to allow us to use + * options as function, which should return array in the format of + * (label, value) pair. + */ + Backform.SelectControl.prototype.render = function() { + var field = _.defaults(this.field.toJSON(), this.defaults), + attributes = this.model.toJSON(), + attrArr = field.name.split('.'), + name = attrArr.shift(), + path = attrArr.join('.'), + rawValue = this.keyPathAccessor(attributes[name], path), + data = _.extend(field, { + rawValue: rawValue, + value: this.formatter.fromRaw(rawValue, this.model), + attributes: attributes, + formatter: this.formatter + }), + evalF = function(f, m) { + return (_.isFunction(f) ? !!f(m) : !!f); + }; + + // Evaluate the disabled, visible, and required option + _.extend(data, { + disabled: evalF(data.disabled, this.model), + visible: evalF(data.visible, this.model), + required: evalF(data.required, this.model) + }); + // Evaluation the options + if (_.isFunction(data.options)) { + try { + data.options = data.options.apply(this) + } catch(e) { + // Do nothing + data = [] + this.model.trigger('pgadmin-view:transform:error', m, self.field, e); + } + } + + // Clean up first + this.$el.removeClass(Backform.hiddenClassname); + + if (!data.visible) + this.$el.addClass(Backform.hiddenClassname); + + this.$el.html(this.template(data)).addClass(field.name); + this.updateInvalid(); + + return this; + }; + var ReadonlyOptionControl = Backform.ReadonlyOptionControl = Backform.SelectControl.extend({ template: _.template([ '',