Monday, 15 July 2013

jquery - Function not defined in Javascript Console -



jquery - Function not defined in Javascript Console -

i don't know why function in console shows not defined. have tried create right , can't seem it. works fine until seek utilize setinterval function. build keeps saying missing semicolon, don't see it.

$(document).ready(function () { var machinedataviewmodel = { machinedataitems: ko.observablearray([]), loadmachinedataitems: function dataload() { machinedataviewmodel.machinedataitems.length = 0; $.getjson("http://localhost/jsonrestful/service1.svc/getmachinedata", function (data) { $.each(data.getmachinedataresult, function (index, item) { machinedataviewmodel.machinedataitems.push(new machinedatamodel(item)); }); }); } }; ko.applybindings(machinedataviewmodel); machinedataviewmodel.loadmachinedataitems(); setinterval(dataload, 9000); }); function machinedatamodel(item) { this.mach_no = ko.observable(item.mach_no), this.var1 = ko.observable(item.var1), this.var2 = ko.observable(item.var2), this.var3 = ko.observable(item.var3), this.var4 = ko.observable(item.var4) };

you can't define dataload() function way , expect available in setinterval(). doesn't work way. symbol dataload available within scope of function. instead, can phone call as:

setinterval(machinedataviewmodel.loadmachinedataitems, 9000);

here's simple demonstration shows can't name function , expect utilize name outside scope: http://jsfiddle.net/jfriend00/6t0pp60s/ (look in debug console see error).

fyi, if need function have right value of this (which don't think do), phone call (with semi-colon @ end of every line):

setinterval(machinedataviewmodel.loadmachinedataitems.bind(machinedataviewmodel), 9000);

as semicolon issue, jshint points this.var4 assignment line. i'd suggest changing machinedatamodel() (which gives clean bill of health in jshint):

function machinedatamodel(item) { this.mach_no = ko.observable(item.mach_no); this.var1 = ko.observable(item.var1); this.var2 = ko.observable(item.var2); this.var3 = ko.observable(item.var3); this.var4 = ko.observable(item.var4); }

javascript jquery ajax knockout.js

No comments:

Post a Comment