Skip to content

Instantly share code, notes, and snippets.

@xomaczar
Last active September 11, 2019 14:02
Show Gist options
  • Save xomaczar/1a916434cde11dd54601a651909e89ce to your computer and use it in GitHub Desktop.
Save xomaczar/1a916434cde11dd54601a651909e89ce to your computer and use it in GitHub Desktop.
cancel-type select
import Ember from 'ember';
export default Ember.Controller.extend({
appName: 'Ember Twiddle',
serializedReasonCode: undefined,
actions: {
reasonCodeChanged({ target }) {
this.model.set('cancelType', target.value);
},
saveReasonCode(reasonCode) {
this.set('serializedReasonCode',
JSON.stringify(reasonCode.serialize()), null, "\t");
}
}
});
import Ember from 'ember';
export function eq([left, right]/*, hash*/) {
return left == right;
}
export default Ember.Helper.helper(eq);
import Model from 'ember-data/model';
import attr from 'ember-data/attr';
import { belongsTo, hasMany } from 'ember-data/relationships';
import { computed } from '@ember/object';
export default Model.extend({
cancelType: attr('number', { defaultValue: 1 }),
cancelTypeName: computed('cancelType', 'cancelTypeCodes.[]', function() {
let t = this.cancelTypeCodes.find(c => c.id == this.cancelType);
return t ? t.name : 'None';
}),
cancelTypeCodes: computed(function() {
return [
{ id: 0, name: 'None' },
{ id: 1, name: 'Pharmacy' },
{ id: 2, name: 'Customer' },
];
}),
});
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.createRecord('reason-code');
}
});
<br>
<p>
Selected <em>ReasonCode's</em> cancelType is {{this.model.cancelTypeName}}
</p>
<select
value={{this.model.cancelType}}
onchange={{action "reasonCodeChanged"}}>
{{#each this.model.cancelTypeCodes as | code |}}
<option selected={{if (eq code.id this.model.cancelType) "selected"}} value={{code.id}}>
{{code.name}}
</option>
{{/each}}
</select>
<br>
<br>
<br>
<button onclick={{action "saveReasonCode" this.model}}> Save </button>
<p>
Serialized Output Just Befor XHR:
<code>
<pre>
{{this.serializedReasonCode}}
</pre>
</code>
</p>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment