mirror of
https://github.com/pgadmin-org/pgadmin4.git
synced 2025-02-10 15:36:06 -06:00
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.
This commit is contained in:
parent
574105ce5e
commit
109b367fc3
@ -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);
|
||||
}
|
||||
|
@ -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([
|
||||
'<label class="<%=Backform.controlLabelClassName%>"><%=label%></label>',
|
||||
|
Loading…
Reference in New Issue
Block a user