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

Commit

Permalink
feat(association-select): Added support for multiple
Browse files Browse the repository at this point in the history
  • Loading branch information
RWOverdijk committed Jan 6, 2016
1 parent e967601 commit f580901
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/component/association-select.html
@@ -1,5 +1,5 @@
<template>
<select class="form-control" value.bind="value">
<select class="form-control" value.bind="value" multiple.bind="multiple">
<option selected disabled value="0">- Select a value -</option>
<option model.bind="option.id" repeat.for="option of options">${option[property]}</option>
</select>
Expand Down
57 changes: 44 additions & 13 deletions src/component/association-select.js
@@ -1,11 +1,11 @@
import {bindable, inject} from 'aurelia-framework';
import {bindingMode, BindingEngine} from 'aurelia-binding';
import {customElement} from 'aurelia-templating';
import {EntityManager, OrmMetadata} from '../index';
import {EntityManager, OrmMetadata, Entity} from '../index';
import extend from 'extend';

@customElement('association-select')
@inject(BindingEngine, EntityManager)
@inject(BindingEngine, EntityManager, Element)
export class AssociationSelect {
@bindable criteria = null;

Expand All @@ -21,31 +21,66 @@ export class AssociationSelect {

@bindable({defaultBindingMode: bindingMode.twoWay}) value;

multiple = false;

ownMeta;

/**
* Create a new select element.
*
* @param {BindingEngine} bindingEngine
* @param {EntityManager} entityManager
* @param {Element} element
*/
constructor(bindingEngine, entityManager) {
constructor(bindingEngine, entityManager, element) {
this._subscriptions = [];
this.bindingEngine = bindingEngine;
this.entityManager = entityManager;
this.multiple = typeof element.getAttribute('multiple') === 'string';
}

/**
* (Re)Load the data for the select.
*
* @param {string|Array} [reservedValue]
*
* @return {Promise}
*/
load() {
load(reservedValue) {
return this.buildFind()
.then(options => {
let result = options;
this.options = Array.isArray(result) ? result : [result];

this.setValue(reservedValue);
});
}

/**
* Set the value for the select.
*
* @param {string|Array} value
*/
setValue(value) {
if (!value) {
return;
}

if (!Array.isArray(value)) {
this.value = value;

return;
}

let selectedValues = [];

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

this.value = selectedValues;
}

/**
* Get criteria, or default to empty object.
*
Expand Down Expand Up @@ -143,13 +178,12 @@ export class AssociationSelect {
*/
attached() {
if (!this.association && !this.manyAssociation) {
this.load();
this.load(this.value);

return;
}

let initialValue = this.value;
this.ownMeta = OrmMetadata.forTarget(this.entityManager.resolveEntityReference(this.repository.getResource()));
this.ownMeta = OrmMetadata.forTarget(this.entityManager.resolveEntityReference(this.repository.getResource()));

if (this.manyAssociation) {
this.observe(this.manyAssociation);
Expand All @@ -159,11 +193,8 @@ export class AssociationSelect {
this.observe(this.association);
}

if (initialValue) {
this.load()
.then(() => {
this.value = initialValue;
});
if (this.value) {
this.load(this.value);
}
}

Expand All @@ -179,7 +210,7 @@ export class AssociationSelect {
let associations = meta.fetch('associations');

return Object.keys(associations).filter(key => {
return associations[key] === resource;
return associations[key].entity === resource;
})[0];
}

Expand Down

0 comments on commit f580901

Please sign in to comment.