gpt4 book ai didi

javascript - 需要 js 和 Knockout js - 无法从服务器获取数据

转载 作者:行者123 更新时间:2023-12-03 08:00:41 24 4
gpt4 key购买 nike

刚接触 require js,仍在学习 knockout js。我在一个现有的项目上实现 require js,但无法让它通过 ajax 获取我的数据。当我尝试运行此行 GDI_Application.fetchdata(); 时,我当前收到错误:Uncaught TypeError: GDI_Application.fetchdata is not a function。我需要启动该功能才能使整个事情变得生动起来。

我尝试通过将 console.logs 放在函数的不同部分来调试代码,但没有一个返回。所以它甚至没有尝试启动它。我什至在控制台中尝试了 incidentViewModel.fetchdata(); 但返回了相同的错误。然后尝试执行 incidentViewModel.fetchdata; 来咯咯笑,但我得到了 undefined

下面是我到目前为止得到的代码。问题似乎出在 Main.js 文件或 GDI_Application 文件上。整个早上都在盯着它看,但没有发现任何明显的东西。

此时正在寻求任何建议。你们认为问题出在哪里?

Main.js文件代码:

require.config({
paths: {
"jqueryUI": "../assets/jqueryUI/jquery-ui.min",
"bootstrap": "bootstrap.min",
"bootstrap_select": "../assets/silviomoreto-bootstrap-select-a8ed49e/dist/js/bootstrap-select.min",
"jquery_timepicker": "jquery-ui-timepicker-addon",
"jqueryui_timepicker_ext": "jquery-ui-sliderAccess",
"moment": "moment",
"cookie": "js.cookie",
"knockout-amd-helpers": "knockout-amd-helpers.min",
"text": "text"
},
"shim": {
bootstrap: {
deps : [ 'jquery'],
exports: 'Bootstrap'
},
bootstrap_select: {
deps : [ 'jquery', 'bootstrap'],
exports: 'Bootstrap_Select'
},
jquery_timepicker: {
deps : [ 'jquery'],
exports: 'Jquery_Timepicker'
},
jqueryui_timepicker_ext: {
deps : [ 'jquery'],
exports: 'Jqueryui_Timepicker_Ext'
}
}
});

require(["knockout", "GDI_Application", "GDI_Buttons", "GDI_common", "knockout-amd-helpers", "text", "moment"], function (ko, GDI_Application) {
ko.amdTemplateEngine.defaultPath = "../templates";

ko.applyBindings(new GDI_Application());
GDI_Application.fetchdata();
});

这是我的 GDI_Application.js 文件:

define(["knockout", "jquery", "jqueryUI", "bootstrap", "bootstrap_select","jquery_timepicker", "jqueryui_timepicker_ext", "moment"], function(ko, $) {

ko.bindingHandlers.modal = {
init: function (element, valueAccessor) {
$(element).modal({
show: false
});

var value = valueAccessor();
if (typeof value === 'function') {
$(element).on('hide.bs.modal', function() {
value(false);
});
}
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
$(element).modal("destroy");
});

},
update: function (element, valueAccessor) {
var value = valueAccessor();
if (ko.utils.unwrapObservable(value)) {
$(element).modal('show');
} else {
$(element).modal('hide');
}
}
}

incidentViewModel = function IncidentViewModel() {
var self = this;
self.showDialog = ko.observable(false);
self.incidents = ko.observableArray();
self.currentIncident = ko.observable();

Incident.BASE_URL = '../../_vti_bin/listData.svc/GDI_DEV_Incidents';
Incident.CREATE_HEADERS = {"accept": "application/json;odata=verbose"};
Incident.UPDATE_HEADERS = {"accept": "application/json;odata=verbose","If-Match": "*"};

self.fetchdata = function() {
console.log("fetching - Attempting to execute code.");
$.getJSON(Incident.BASE_URL+filterlist+orderlist,
function(data) {
if (data.d.results) {
self.incidents(data.d.results.map(function(item) {
return new Incident(item);
}));
$('#loading').hide("slow");
$('#IncidentTable').show("slow");
console.log("fetching data completed");
}else {
console.log("no results received from server");
}
});
}

self.saveorupdate = function() {
console.log("save function executed");
var id = this.ID,
url = Incident.BASE_URL + (id ? '(' + encodeURIComponent(id) + ')' : '');
console.log(url);
return $.ajax(url, {
type: id ? "MERGE" : "POST",
data: ko.toJSON({
Description: this.Description,
Incident: this.Incident
}),
processData: false,
contentType: "application/json;odata=verbose",
headers: id ? Incident.UPDATE_HEADERS : Incident.CREATE_HEADERS,
success: function (data) {
incidentViewModel.fetchdata();
console.log("Record was sucessfully saved.");

}
});
}

self.ShowSelectedIncident = function(data) {
self.currentIncident(data);
self.showDialog(true);
console.log("The show selected incident has been ran.");
}

self.clearCurrentIncident = function() {
self.showDialog(false);
self.currentIncident(null);
}

self.AddNewIncident = function() {
self.showDialog(true);
self.currentIncident({ID:"",Description:"",Incident:""});
}
}

function Incident(data) {
var self = this;
self.ID = data.ID;
self.Description = ko.observable(data.Description);
self.Composante = ko.observable(data.Composante);
self.Incident = ko.observable(data.Incident);
self.ÉtatValue = ko.observable(data.ÉtatValue);
self.PrioritéValue = ko.observable(data.PrioritéValue);
self.Duré = ko.observable(data.Duré);
self.Service = ko.observable(data.Service);
self.Début_imputabilité = ko.observable(data.Début_imputabilité);
self.Début_de_interruption = ko.observable(data.Début_de_interruption);
self.Fin_de_interruption = ko.observable(data.Fin_de_interruption);
self.Groupe_Support_Prime = ko.observable(data.Groupe_Support_Prime);
self.ResponsableValue = ko.observable(data.ResponsableValue);
self.Impact = ko.observable(data.Impact);
self.Dépanage = ko.observable(data.Dépanage);
self.Suivi = ko.observable(data.Suivi);
self.Ressources = ko.observable(data.Ressources);
}

return incidentViewModel;
});

最佳答案

从代码看,fetchdata并不是静态调用。它需要一个实例。更改以下代码:

ko.applyBindings(new GDI_Application());
GDI_Application.fetchdata();

至:

var app = new GDI_Application();
ko.applyBindings(app);
app.fetchdata();

希望这有帮助。

关于javascript - 需要 js 和 Knockout js - 无法从服务器获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34593312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com