Allow to use the url with/without the node id in the node ajax options

control, also - allow to specify the filter function in the nodes-by-id,
and nodes-by-name control.
This commit is contained in:
Ashesh Vashi 2016-01-04 12:43:24 +05:30
parent f451bd0a83
commit aedf970748

View File

@ -20,7 +20,8 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) {
Backform.SelectControl.extend({ Backform.SelectControl.extend({
defaults: _.extend(Backform.SelectControl.prototype.defaults, { defaults: _.extend(Backform.SelectControl.prototype.defaults, {
url: undefined, url: undefined,
transform: undefined transform: undefined,
url_with_id: false
}), }),
initialize: function() { initialize: function() {
/* /*
@ -42,7 +43,7 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) {
full_url = node.generate_url.apply( full_url = node.generate_url.apply(
node, [ node, [
null, url, this.field.get('node_data'), null, url, this.field.get('node_data'),
false, this.field.get('node_info') this.field.get('url_with_id') || false, this.field.get('node_info')
]), ]),
/* /*
* We needs to check, if we have already cached data for this url. * We needs to check, if we have already cached data for this url.
@ -111,19 +112,26 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) {
first_empty: true, first_empty: true,
empty_value: '-- None --', empty_value: '-- None --',
url: 'nodes', url: 'nodes',
filter: undefined,
transform: function(rows) { transform: function(rows) {
var self = this, var self = this,
node = self.field.get('schema_node'), node = self.field.get('schema_node'),
res = []; res = [],
filter = self.field.get('filter') || function() { return true; };
_.each(rows, function(r) { _.each(rows, function(r) {
res.push({ if (filter(r)) {
'value': r._id, var l = (_.isFunction(node['node_label']) ?
'node': _.isFunction(node['node_image']) ? (node['node_image']).apply(node, [r, self.model]) : node.type, (node['node_label']).apply(node, [r, self.model, self]) :
'label': (_.isFunction(node['node_label']) ? r.label),
(node['node_label']).apply(node, [r, self.model]) : image: (_.isFunction(node['node_image']) ?
r.label) (node['node_image']).apply(node, [r, self.model, self]) : node.type);
}); res.push({
'value': r._id,
'node': image,
'label': l
});
}
}); });
return res; return res;
@ -137,17 +145,22 @@ function($, _, pgAdmin, Backbone, Backform, Alertify, Node) {
transform: function(rows) { transform: function(rows) {
var self = this, var self = this,
node = self.field.get('schema_node'), node = self.field.get('schema_node'),
res = []; res = [],
filter = self.field.get('filter') || function() { return true; };
_.each(rows, function(r) { _.each(rows, function(r) {
var l = _.isFunction(node['node_label']) ? if (filter(r)) {
(node['node_label']).apply(node, [r, self.model]) : var l = (_.isFunction(node['node_label']) ?
r.label; (node['node_label']).apply(node, [r, self.model, self]) :
res.push({ r.label),
'value': l, image: (_.isFunction(node['node_image']) ?
'node': _.isFunction(node['node_image']) ? (node['node_image']).apply(node, [r, self.model]) : node.type, (node['node_image']).apply(node, [r, self.model, self]) : node.type);
'label': l res.push({
}); 'value': l,
'node': image,
'label': l
});
}
}); });
return res; return res;