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:
Ashesh Vashi 2016-01-04 11:34:40 +05:30
parent 574105ce5e
commit 109b367fc3
2 changed files with 55 additions and 7 deletions

View File

@ -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);
}

View File

@ -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>',