Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
fix(association-select): use custom id property
Browse files Browse the repository at this point in the history
  • Loading branch information
doktordirk committed Jul 25, 2016
1 parent 37fe2d9 commit 5e4b49d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
6 changes: 6 additions & 0 deletions doc/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ This is the selected value of the element. This functions the same as a regular
### error.bind
That's where a server response error would be stored.

### identifier
This tells the component which property to use from the select's value. **Defaults to `id`**.

### property
This tells the component which property to use from the data sent back by the resource (using the repository). **Defaults to `name`**.

Expand All @@ -99,6 +102,9 @@ Almost exactly the same as the `association` attribute, except for a `many` asso

_This attribute does **not** accept arrays, but can be combined with the `association` attribute_.

### multiple
This sets the component to a multi-select. **Defaults to `false`**.

### criteria.bind
Pass along filter criteria (as JSON or Object) to the element. These will be used to restrict the data returned from the API.

Expand Down
20 changes: 10 additions & 10 deletions src/component/association-select.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class AssociationSelect {

@bindable repository;

@bindable identifier;
@bindable identifier = 'id';

@bindable property = 'name';

Expand Down Expand Up @@ -55,7 +55,7 @@ export class AssociationSelect {
/**
* (Re)Load the data for the select.
*
* @param {string|Array} [reservedValue]
* @param {string|Array|Object} [reservedValue]
*
* @return {Promise}
*/
Expand All @@ -72,23 +72,23 @@ export class AssociationSelect {
/**
* Set the value for the select.
*
* @param {string|Array} value
* @param {string|Array|Object} value
*/
setValue(value) {
if (!value) {
return;
}

if (!Array.isArray(value)) {
this.value = (typeof value === 'object') ? getProp(value, this.identifier || 'id') : value;
this.value = (typeof value === 'object') ? getProp(value, this.identifier) : value;

return;
}

let selectedValues = [];

value.forEach(selected => {
selectedValues.push(selected instanceof Entity ? selected.id : selected);
selectedValues.push(selected instanceof Entity ? selected.getId() : selected);
});

this.value = selectedValues;
Expand Down Expand Up @@ -128,12 +128,12 @@ export class AssociationSelect {
delete criteria.populate;

let property = this.propertyForResource(assoc.getMeta(), repository.getResource());
findPath = `${assoc.getResource()}/${assoc.id}/${property}`;
findPath = `${assoc.getResource()}/${assoc.getId()}/${property}`;
} else if (this.association) {
let associations = Array.isArray(this.association) ? this.association : [this.association];

associations.forEach(association => {
criteria[this.propertyForResource(this.ownMeta, association.getResource())] = association.id;
criteria[this.propertyForResource(this.ownMeta, association.getResource())] = association.getId();
});
}

Expand All @@ -147,14 +147,14 @@ export class AssociationSelect {
*/
verifyAssociationValues() {
if (this.manyAssociation) {
return !!this.manyAssociation.id;
return !!this.manyAssociation.getId();
}

if (this.association) {
let associations = Array.isArray(this.association) ? this.association : [this.association];

return !associations.some(association => {
return !association.id;
return !association.getId();
});
}

Expand All @@ -175,7 +175,7 @@ export class AssociationSelect {
return this;
}

this._subscriptions.push(this.bindingEngine.propertyObserver(association, 'id').subscribe(() => {
this._subscriptions.push(this.bindingEngine.propertyObserver(association, association.getIdProperty()).subscribe(() => {
if (this.verifyAssociationValues()) {
return this.load();
}
Expand Down

0 comments on commit 5e4b49d

Please sign in to comment.