After completing coding of chapter 10, i am getting an error which i am not able to resolve.
Uncaught TypeError: FormHandler is not a constructor
at main.js:11
at main.js:15
anyhelp would be highly appreciated.
main.js:
(function (window) {
‘use strict’;
var FORM_SELECTOR = ‘[data-coffee-order=“form”]’;
var App = window.App;
var Truck = App.Truck;
var DataStore = App.DataStore;
var FormHandler = App.FormHandler;
var myTruck = new Truck(‘ncc-1701’, new DataStore());
window.myTruck = myTruck;
var formHandler = new FormHandler(FORM_SELECTOR);
formHandler.addSubmitHandler(myTruck.createOrder.bind(myTruck));
console.log(formHandler);
})(window);
formHander.js:
(function (window){
‘use strict’;
var App = window.App || {};
var $ = window.jQuery;
function FormHandler(selector){
if (!selector){
throw new Error('No selector provided');
}
this.$formElement = $(selector);
if (this.$formElement.length === 0 ){
throw new Error('Could not find element with selector: '+ selector);
}
}
FormHandler.prototype.addSubmitHandler = function (fn){
console.log('Setting submit handler to form');
this.$formElement.on('submit', function(event){
event.preventDefault();
var data = {};//$(this).serializeArray();
$(this).serializeArray().forEach(function (item){
data[item.name] = item.value;
console.log(item.name + ' is '+ item.value);
})
console.log(data);
fn(data);
this.reset();
this.element[0].focus();
});
}
App.FormHandler = FormHandler;
window.App = App;
Hello @shashibond, @davedauenhauer, and @sho! I am having the same problem, i.e. I am getting the error “App.FormHandler is not a constructor.” My javascript files are loaded in the order indicated in the “solution.”
Hi @dartagnan, to debug this issue, try running console.log(App.FormHandler) in the developer console. Most likely, it’ll say “undefined”, which means you probably aren’t assigning to App.FormHandler. That could happen in formhandler.js, you might have a simple typo when setting App.FormHandler = ... or you might not be setting window.App = App to export App as a global variable.
If that doesn’t help, feel free to paste the contents of formhandler.js and main.js here!
Hi @nybblr, @brianball, sorry for my delay in responding. I have been sick with a viral infection. Didn’t feel up to coding.
@brianball, I changed the order of the script files as you suggested. Still getting the “…is not a constructor” error.
Per @nybblr, I am posting formhandler.js and main.js.
(function (window) {
‘use strict’;
var App = window.App || {};
var $ = window.jQuery;
function FormHandler(selector) {
if (!selector) {
throw new Error(‘No selector provided’);
}
this.$formElement = $(selector);
};
App.FormHandler = FormHandler;
window.App = App;
})(window);
Here is main.js…
(function (windows) {
‘use strict’;
var App = window.App;
var Truck = App.Truck;
var DataStore = App.DataStore;
var myTruck = new Truck(‘ncc-1701’, new DataStore());
window.myTruck = myTruck;
})(window);
Hi @dartagnan, just got back in town myself. Hope you’re feeling better! Did you figure out what was going on?
Could you post a screenshot of the console when you load the page? Make sure you aren’t filtering console messages (that way we can make sure all your tags are loading). You might also stick a console.log inside formhandler to confirm that the code inside the IIFE is/isn’t executing.