Thursday 22 December 2016

javascript - Uncaught TypeError: Cannot read property 'addNewDevice' of undefined

I have the following class declarations:



class DevicesModel{
constructor(){
//khoi tao devices
this.devices = [];
this.tt = 1;
}
addNewDevice(ma,ten,dvt,trongLuong){

const device = {tt: this.tt,ma,ten,dvt,trongLuong};
this.tt += 1;
this.devices.push(device);
return device;
}
}
class DevicesController{
constructor(){
this.devicesModel = new DevicesModel();
this.devicesView = new DevicesView();

}
addNewDevice(){
const maSo = document.getElementById("ma-sp").value;
const ten = document.getElementById("ten-sp").value;
const dvt = document.getElementById("dvt").value;
const trongLuong = document.getElementById("trong-luong").value;
if (maSo === '' || ten === '' || dvt === '' || trongLuong === '') {
return;
}
//error is here

this.devicesModel.addNewDevice(maSo,ten,dvt,trongLuong);
this.devicesView.addNewDevice(maSo,ten,dvt,trongLuong);
this.tt += 1;
//dong modal
document.querySelector('.bg-modal').style.display = "none";
}
start(){
this.devicesView.showHideModal();
document.getElementById("save").addEventListener('click',this.addNewDevice);
}

}
const devicesController = new DevicesController();
devicesController.start();


When I try to add new device the this.devicesModel returns that error. I don't know what is the error in my code.



I would really appreciate any help. Thank you!

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...