Ad

KNockout JS - Automatic Reload Of Ajax Call

- 1 answer

Hi folks trying to get a simple setintervel working to automatically refresh my data every minute. The line im having issues with is this:

setInterval(incidentViewModel.fetchdata,60000);

I had also tried this:

window.setInterval(incidentViewModel.fetchdata,60000);

Both of these give me the same error:

Uncaught TypeError: self.incidents is not a function

Im not seeing anything obvious that's causing the problem, would anyone have any idea?

Here is my full code:

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);
Ad

Answer

Remove var self = this; inside the self.fetchdata function because you should be referring to the self inside the IncidentViewModel function when referring to self.incidents().

Ad
source: stackoverflow.com
Ad