Nie dostaję obserwowanych do ucieczki. Jest klasa dla urządzeń. W tej klasie muszę pobrać kilka zmiennych z żądaniem $ .get. To żądanie pobierze dane ze skryptu .php, który zbiera dane z bazy danych MySQL.
Błąd to „this.device nie jest konstruktorem”. Myślę, że nie ma go w zakresie funkcji. Czy somene może mi podpowiedzieć? Zwykle jestem w domu w C #, przepraszam :)
Wsparcie?
var link150 = function() {
//Array zum Sammeln der Devices
this.devices = [];
//#####################
//Definition eines Devices
//#####################
function general_config() {
this.database_id = "";
this.devicename = "";
this.hardware_id = "";
this.serialnumber = "";
this.ip = "";
this.firmware_version = "";
this.model = "";
this.password = "";
}
this.device = function() {
this.database_id = "";
this.devicename = "";
this.username = "";
this.hardware_id = "";
this.fqdn = "";
this.kunde_id = "";
this.location = "";
this.ip = "";
this.defaultPW = "";
this.generalConfig = new general_config(); //TEST
this.generalConfig.model ="EGX150"; //TEST
}
jQuery.ajaxSetup({async:false});
//#####################
//Devices aus Datenbank laden
//#####################
this.loadDevices = function() {
_devices = [];
$.get('php/abfrage.php', {action:'loadDeviceList'}, function(data){
$.each(JSON.parse(data), function(i, item) {
tmp = new this.device();
tmp = {
id: item.id,
hardware_id: item.hd_id,
devicename: item.devicename,
fqdn: item.fqdn,
kunde_id: item.kunde_id,
location: item.kunde_location,
ip: item.ip,
lastsid: item.lastSID
};
_devices.push(tmp);
});
})
.always(function(){
hide_loader();
});
this.devices = _devices;
};
//#####################
//Geräte laden
//#####################
this.loadDevices();
jQuery.ajaxSetup({async:true});
};
1
h0d3nt3uf3l
5 marzec 2020, 11:11
3 odpowiedzi
Najlepsza odpowiedź
Czy to działa?
var _self=this;
this.loadDevices = function() {
_devices = [];
$.get('php/abfrage.php', {action:'loadDeviceList'}, function(data){
$.each(JSON.parse(data), function(i, item) {
tmp = new _self.device();
tmp = {
id: item.id,
hardware_id: item.hd_id,
devicename: item.devicename,
fqdn: item.fqdn,
kunde_id: item.kunde_id,
location: item.kunde_location,
ip: item.ip,
lastsid: item.lastSID
};
_devices.push(tmp);
});
})
.always(function(){
hide_loader();
});
this.devices = _devices;
};
1
waiaan
5 marzec 2020, 08:20
Dzięki, to rozwiązało mój problem, ale widzę tylko kolejny „problem”. Z twoim rozwiązaniem klasa zostałaby osiągnięta i mogę z nią pracować, ale muszę ustawić wszystkie właściwości z nazwą zmiennej tmp.
var _self=this;
this.loadDevices = function() {
_devices = [];
$.get('php/abfrage.php', {action:'loadDeviceList'}, function(data){
$.each(JSON.parse(data), function(i, item) {
tmp = new _self.device();
tmp.id = item.id;
tmp.hardware_id = item.hd_id;
tmp.devicename = item.devicename;
tmp.fqdn = item.fqdn;
tmp.kunde_id = item.kunde_id;
tmp.location = item.kunde_location;
tmp.ip = item.ip;
tmp.lastsid = item.lastSID;
_devices.push(tmp);
});
})
.always(function(){
hide_loader();
});
this.devices = _devices;
};
Czy jest lepsza składnia, jak w C #?
tmp = new class() {
item1 = "somevar",
item2 = "somevar2"
}
//Instead
tmp = new class();
tmp.item1 = "somevar";
tmp.item2 = "somevar2";
0
h0d3nt3uf3l
5 marzec 2020, 08:30
this.device = function(database_id,devicename,...) {
this.database_id = database_id;
this.devicename = devicename;
...
}
tmp = new _self.device(item.id,item.hd_id,...);
0
waiaan
5 marzec 2020, 08:35