Permalink
Browse files

feat(association-select): Added support for `multiple`

  • Loading branch information...
RWOverdijk committed Jan 6, 2016
1 parent e967601 commit f5809016b730a4bcac5e6749b1168ccdad2b89db
Showing with 45 additions and 14 deletions.
  1. +1 −1 src/component/association-select.html
  2. +44 −13 src/component/association-select.js
@@ -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>
@@ -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;
@@ -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.
*
@@ -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);
@@ -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);
}
}
@@ -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];
}

0 comments on commit f580901

Please sign in to comment.