Syntactic sugar for setting multiple properties in registry

Added setProperties that allow a descriptor object to pass in. If the value
is a string, assume that it is a label. See example in doc and test for usage.

Change-Id: I7211c52e2f128c2c3174625f8670d3929be57397
This commit is contained in:
Thai Tran 2016-08-15 10:16:05 -07:00
parent 450baeed4f
commit 3027c99740
2 changed files with 37 additions and 1 deletions

View File

@ -100,6 +100,7 @@
self.type = typeCode;
self.initActions = initActions;
self.setProperty = setProperty;
self.setProperties = setProperties;
self.getProperties = getProperties;
self.getName = getName;
self.setNames = setNames;
@ -193,6 +194,31 @@
return self;
}
/**
* @ngdoc function
* @name setProperties
* @description
* Syntactic sugar for setProperty.
* Allows an object of properties where the key is the id and the value
* can either be a string or an object. If the value is a string, we assume
* that it is the label. If the value is an object, we use the object as
* the property for that key.
* @example
```
var properties = {
id: gettext('ID'),
enabled: { label: gettext('Enabled') }
};
resourceType.setproperties(properties);
*/
function setProperties(properties) {
angular.forEach(properties, function(value, key) {
var prop = angular.isString(value) ? { label: value } : value;
setProperty(key, prop);
});
return this;
}
/**
* Return a copy of any properties that have been registered.
* @returns {*}

View File

@ -78,9 +78,14 @@
describe('label', function() {
var label;
beforeEach(function() {
var properties = {
id: 'eyedee',
bd: { label: 'beedee' }
};
var value = service.getResourceType('something', {})
.setProperty('example', {label: gettext("Example")})
.setProperty('bad_example', {});
.setProperty('bad_example', {})
.setProperties(properties);
label = value.label;
});
@ -95,6 +100,11 @@
it('returns the nice label if there is one', function() {
expect(label('example')).toBe('Example');
});
it('returns the properties set via the properties descriptor', function() {
expect(label('id')).toBe('eyedee');
expect(label('bd')).toBe('beedee');
});
});
describe('getName', function() {