KNockout JS - 自动重载ajax调用

时间:2015-12-15 00:51:02

标签: 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);

1 个答案:

答案 0 :(得分:1)

删除error: Parse::InvalidSessionError (Code: 251, Version: 1.11.0)函数中的var self = this;,因为在引用self.fetchdata时,您应该引用IncidentViewModel函数中的self。