A Select component based on the native html select.
Why a select component? Because Ember.Select
is hard to use, and
really, all you want to do is just use a <select>
tag dammit. The
only difference is that you want value of the options to be any object
and not just a string.
ember install emberx-select
By allowing arbitrary html to appear in the template of the select
element, you can use it just like you would normally. This means
things like having <optgroup>
tags inside your select, or even plain
old <option>
elements to represent things like empty values.
XSelect thinly wraps a native <select>
element so that it can be object
and binding aware. It is used in conjuction with the x-option
component to construct select boxes. E.g.
the options are always up to date, so that when the object bound to
value
changes, the corresponding option becomes selected.
Whenever the select tag receives a change event, it will fire
action
.
If you're just changing a model's property, you don't need action
. For example,
if you have a model with a status
field with an integer, you can do this:
As of version 1.1.0, emberx-select
supports the multiple
option. This means you can pass an array as its value, and it will set
its selections directly on that array.
The selections array will be initialized to an empty array if not present.
The action that is dispatched by x-select whenever the selected value or values change has a function signature of:
/**
* @param value {Object} the value selected by the user.
* @param component {Ember.Component} the x-select component itself
*/
function (value, component) {
// action body...
}
Most of the time all you need is the value that has been selected, but sometimes your action requires more context than just that. In those cases, you can associate arbitrary attributes with the component itself and use them later inside your action handler. For example:
then, inside your action handler:
export default Ember.Route.extend({
actions: {
didMakeSelection: function(selection, component) {
if (selection) {
this.set('selection', selection)
} else {
this.set('selection', component.get('default'))
}
}
}
});
Since emberx-select
uses internal identifiers as the value
attribute, it
doesn't integrate with the fillIn
test helper. But don't fret, we've automatically
injected the test helper into your app.
As of version 1.1.3 we support both multiselects and regular selects. To use, you need to specify the class on the select as the first argument and the rest of the arguments are the options you'd like to select. For example:
//... Single select
select('.my-drop-down', 'My Option');
//...
Multiselect
//... Multiselect
select('.my-drop-down', 'My Option', 'My Option Two', 'My Option Three');
//...
You need to run the generator: ember g emberx-select
You need to either run the generator (ember g emberx-select
) or import the test helper into your
test-helper.js
file:
import registerSelectHelper from './helpers/register-select-helper';
registerSelectHelper();
emberx-select is part of the "missing components of ember" collectively known as emberx:
ember test
ember test --server