javascript - KNockout JS - 自动重新加载 ajax 调用

标签 javascript jquery ajax knockout.js

大家好,尝试使用一个简单的 setintervel 来每分钟自动刷新我的数据。我遇到问题的行是:

setInterval(incidentViewModel.fetchdata,60000);

我也尝试过这个:

window.setInterval(incidentViewModel.fetchdata,60000);

这两个都给了我同样的错误:

Uncaught TypeError: self.incidents is not a function

我没有看到任何明显的问题,有人知道吗?

这是我的完整代码:

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

    self.fetchdata = function() {
    Incident.BASE_URL = '../../../../_vti_bin/listData.svc/GDI_PROD_Incidents';
    Incident.CREATE_HEADERS = {"accept": "application/json;odata=verbose"};
    Incident.UPDATE_HEADERS = {"accept": "application/json;odata=verbose","If-Match": "*"}; 

    var self = this;
    $.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");
            }   else {
                    console.log("no results received from server");
                }
        }).fail(function() {
            console.log("error", params, arguments);
    });
    console.log("Im done fetching data, pheww!");
    }  
}

function DataItem(data) {
    //console.log(data);
    this.Name = ko.observable(data.Name);
    this.Price = ko.observable(data.Price);
}

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.Date_de_début = ko.observable(data.Date_de_début);
    self.Date_de_fin = ko.observable(data.Date_de_fin);
    self.Groupe_Support_Prime = ko.observable(data.Groupe_Support_Prime);
    self.Autres_Groupe_Support_Prime = ko.observable(data.Autres_Groupe_Support_Prime);
    self.ResponsableValue = ko.observable(data.ResponsableValue);
    self.Impact = ko.observable(data.Impact);
    self.Temps_Consacré = ko.observable(data.Temps_Consacré);
    self.Type_de_tempsValue = ko.observable(data.Type_de_tempsValue);
    self.Journal_des_actions = ko.observable(data.Journal_des_actions);
    self.Dépanage = ko.observable(data.Dépanage);
    self.Journal_des_actions = ko.observable(data.Journal_des_actions);
    self.Suivi = ko.observable(data.Suivi);
    self.Ressources = ko.observable(data.Ressources); 
}

var incidentViewModel = new IncidentViewModel();
ko.applyBindings(incidentViewModel); 
setInterval(incidentViewModel.fetchdata,60000);

最佳答案

删除 self.fetchdata 函数内的 var self = this;,因为在引用时您应该引用 IncidentViewModel 函数内的 self到self.incidents()

关于javascript - KNockout JS - 自动重新加载 ajax 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34279253/

相关文章:

javascript - 为什么我使用 Knockout JS 时会收到 "Cannot read property ' nodeType' of null"错误?

c# - Request.Files 始终为空

javascript - 处理二维数组中的 json 响应

javascript - 通过 IE 和 Edge 中的快捷键将焦点放在输入字段上

javascript - 如何使用正则表达式从字符串中提取 CSS 规则

javascript - Handlebars : multiple conditions IF statement?

javascript - 我怎样才能结合这两个jquery计算?

javascript - 将输入值属性设置为带空格的变量。

javascript - 如何在点击广告后删除包含广告代码的 div?

javascript - 通过AJAX上传的二进制数据保存在PHP服务器上