decide.nolog.cz/app/components/create-options-text.js
Jeldrik Hanschke b421d19601
refactor step management and do not allow going forward with invalid state (#263)
Also removes an observer that causes a "You modified 'disabled' twice in a single render" and executes the logic in the next run loop to prevent that error. That's not ideal but it's not time for a major refactoring of that part.
2019-10-29 08:42:00 +01:00

44 lines
1 KiB
JavaScript

import { inject as service } from '@ember/service';
import Component from '@ember/component';
import { next } from '@ember/runloop';
export default Component.extend({
actions: {
addOption(element) {
let fragment = this.store.createFragment('option');
let options = this.options;
let position = this.options.indexOf(element) + 1;
options.insertAt(
position,
fragment
);
},
deleteOption(element) {
let position = this.options.indexOf(element);
this.options.removeAt(position);
}
},
enforceMinimalOptionsAmount() {
let options = this.options;
while (options.length < 2) {
options.pushObject(
this.store.createFragment('option')
);
}
},
store: service('store'),
init() {
this._super(...arguments);
// need to delay pushing fragments into options array to prevent
// > You modified "disabled" twice on <(unknown):ember330> in a single render.
// error.
next(() => {
this.enforceMinimalOptionsAmount();
});
}
});