My power-select will not display the selected value.
I have three power-selects in this component and two are working fine, but the third (companyType) is acting strangely - the value is blank when you first go to the form. When you select a value it sticks for that record from then on (until you refresh the page)
Template:
// templates/components/companies/edit-new-form.hbs
{{#bs-form formLayout=formLayout model=company onSubmit=(action "save") as |form|}}
<label>State:
{{#power-select
selected=company.state
options=states
onchange=(action "chooseState")
as |name|
}}
{{name}}
{{/power-select}}
</label>
<label>County:
{{#power-select
selected=company.county
options=countyNames
onchange=(action "chooseCounty")
as |name|
}}
{{name}}
{{/power-select}}
</label>
<label>Company Type:
{{#power-select class="select"
selected=company.companyType
options=companyTypes
onchange=(action "chooseCompanyType")
as |name|
}}
{{name.companyType}}
{{/power-select}}
</label>
{{/bs-form}}
JS:
// componnents/companies/edit-new-form.js
export default Ember.Component.extend({
company: null,
router: Ember.inject.service('-routing'),
ajax: Ember.inject.service(),
store: Ember.inject.service(),
countyNames: Ember.computed('company.state', function() {
return this.get('ajax').request(config.host + '/api/counties/' + this.company.get('state')).then( (json) => json.map( (county) => {
return county.countyName;
})
)
}),
states: ['AK', 'AL', 'AR', 'AZ', 'CA', 'CO', 'CT', 'DC', 'DE', 'FL', 'GA', 'HI', 'IA', 'ID', 'IL', 'IN', 'KS', 'KY', 'LA', 'MA', 'MD', 'ME', 'MI', 'MN', 'MO', 'MS', 'MT', 'NC', 'ND', 'NE', 'NH', 'NJ', 'NM', 'NV', 'NY', 'OH', 'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VA', 'VT', 'WA', 'WI', 'WV', 'WY'],
actions: {
// For dropdown handling
chooseState(state) {
this.set('company.state', state);
//Ember.set(this, "counties", this.get('ajax').request(config.host + '/api/counties/' + state).then((json) => json.items));
//this.set('counties', this.get('ajax').request(config.host + '/api/counties/' + state).then((json) => json));
},
chooseCounty(countyName) {
this.set('company.county', countyName);
},
chooseCompanyType(companyType) {
this.set('company.companyType', companyType);
},
}
});
And here is the route and model hook:
// routes/companies/edit.js
export default Ember.Route.extend({
model(params) {
var store = this.store;
return Ember.RSVP.hash({
companies: store.findRecord('company', params.company_id),
companyTypes: store.findAll('companyType')
});
}
});
The route that gets the companyType model is:
// routes/company-types.js
export default Ember.Route.extend({
model() {
return this.get('store').findAll('company-type');
}
});
And my models:
// models/company.js
export default DS.Model.extend({
companyName: DS.attr('string'),
street: DS.attr('string'),
county: DS.attr('string'),
city: DS.attr('string'),
state: DS.attr('string'),
zip: DS.attr('string'),
phone: DS.attr('string'),
email: DS.attr('string'),
fax: DS.attr('string'),
companyType: DS.attr('string')
});
// models/company-type.js
export default DS.Model.extend({
companyType: DS.attr('string')
});
Looking at Ember Inspector the data for the companyType is there and is a string, per my model. When I select a value from the dropbox, the data type turns into <svegbackend@model:company-type::ember1326:Environ Engineer
. When I select one of the other drop downs, the value stays a string which I guess makes sense since the source for those are string arrays.
So how do I get the drop down to display the value when the form loads?
The problem is that, the companyType
of the company
is of type string where as the options list of the third select are of type companyType
model. This creates a mismatch in setting the selected option initially.
Define the third power select as follows:
{#power-select class="select"
selected=company.companyType
options=companyTypesOptions
onchange=(action "chooseCompanyType")
as |name|
}}
{{name}}
{{/power-select}}
and define companyTypesOptions
within edit-new-form.js
as follows:
companyTypesOptions: Ember.computed('companyTypes.[]', function() {
return this.get('companyTypes').map((companyType)=> {
return Ember.get(companyType, 'companyType');
});
})
By this way; you will make both the options list and the selected value of type string! Do not forget if the type of the options and the selected you pass are different; you will get into trouble! Try to avoid that!