hino / app / components / cost-center / cost-center-popup.js
cost-center-popup.js
Raw
import Component from '@ember/component';
import { inject as service } from '@ember/service';

export default Component.extend({
    destinationInfoModal : service(),

    showModal : false,
    formSubmitted : false,
    model : null,
    didValidate: false,
    showCalendar : false,

    init() {
        this._super(...arguments);
        if (this.model == null) {
            this.set("model" , this.destinationInfoModal.addNewData());
        }
    },

    title : "Cost Center",

    actions: {
        toggleCalendar() {
            let prevValue = this.get("showCalendar");
            this.set("showCalendar", !prevValue);
        },
        setDate(propertyName, date) {
            this.set(propertyName, date);
        },
        submit() {
            this.get('model')
                .validate()
                .then(({ validations }) => {
                    this.set("didValidate", true);
                    if (validations.get('isValid')) {
                        this.set("formSubmitted", true);
                        this.destinationInfoModal.addRow(this.model);
                        this.destinationInfoModal.hide();
                    }
                });
        },
        hideModal() {
            if (!this.formSubmitted) {
                let object = this.destinationInfoModal.oldModel;
                if (object != null) {
                    object = object.toJSON();
                    Object.getOwnPropertyNames(object).forEach(key => {
                        this.set("model." + key, object[key]);
                    });
                    this.destinationInfoModal.addRow(this.model);
                }
            }
            this.set("showModal", false);
            this.set("formSubmitted", false);
            this.set("destinationInfoModal.oldModel", null);
            this.destinationInfoModal.hide();

        }
    }
});