| .. | .. |
|---|
| 134 | 134 | this.query = function() { |
|---|
| 135 | 135 | return this.getResource().query({}, _success, _fail); |
|---|
| 136 | 136 | } |
|---|
| 137 | + this.refreshRef = function(refs, res, preloadedData) { |
|---|
| 138 | + // We check if there is some field for the resource passed as parameter |
|---|
| 139 | + var field = (function() { |
|---|
| 140 | + for (var i = _current.fields.length - 1; i >= 0; i--) { |
|---|
| 141 | + if (_current.fields[i].resource === res) |
|---|
| 142 | + return _current.fields[i]; |
|---|
| 143 | + } |
|---|
| 144 | + return null; |
|---|
| 145 | + })(); |
|---|
| 146 | + |
|---|
| 147 | + // If field for that resource is not found there is nothing to refresh |
|---|
| 148 | + if (!field) return; |
|---|
| 149 | + var resource = this.getResource(res); |
|---|
| 150 | + var data = preloadedData || resource.query({}, _success, _fail); |
|---|
| 151 | + var that = this; |
|---|
| 152 | + data.$promise.then(function(responseData) { |
|---|
| 153 | + var pk = that.getPk(that.getMetadata(field.resource)) |
|---|
| 154 | + var comboData = [] |
|---|
| 155 | + responseData.forEach(function(row) { |
|---|
| 156 | + comboData.push({ |
|---|
| 157 | + id: row[pk], |
|---|
| 158 | + label: row.label || row.name || row.code || row.first_name + ' ' + row.last_name |
|---|
| 159 | + }); |
|---|
| 160 | + }) |
|---|
| 161 | + refs[field.name] = comboData; |
|---|
| 162 | + }) |
|---|
| 163 | + } |
|---|
| 137 | 164 | this.loadRefs = function(refs) { |
|---|
| 138 | 165 | if (!_current) throw new Error('There is no current catalog selected'); |
|---|
| 139 | 166 | var refsFields = []; |
|---|