class ProductForm { constructor() { this.$supplierField = $("#supplier-id"); this.$typeField = $("#type"); this.$skuField = $("#sku"); this.$supplierField.on("change", () => this.autoFillSku()); this.$typeField.on("change", () => this.autoFillSku()); this.autoFillSku(); } get isNew() { return $("#new").val() === "1"; } autoFillSku() { if(this.$skuField.val() && !this.isNew) { return; } $.post("/api/suggest-sku", { supplier_id: this.$supplierField.val(), product_type: this.$typeField.val(), }).then(response => { if(response.sku) { this.$skuField.val(response.sku); } }).catch(error => { this.$skuField.val(""); }); } } $(() => new ProductForm());