0

I'm trying to call a function of the object from another function and I get "Undefined is not a function" error. I think there's some silly mistake but I can't figure it out.

function dataProvider() {

    var self = this;

    self.loadData = ko.computed(function(){

        var data = new Data(self.getJson().data); //here comes the error
        ...


    });

    self.getJson = function(){
        if(typeof jsonVar !== 'undefined')
            return jsonVar;
        else {
            ...
        }


    };

}

The jsonVar is defined and it is a valid JSON. Data is an object, but self.getJson() just doesn't work.

EDIT: It is not about calling private or public method. The problem is that I can't call the method of the object.

DropDropped
  • 1,253
  • 1
  • 22
  • 50
  • Are you sure, the error occurs on exact that line of code? What about `ko.computed`? Is this really defined? Anyway, try to add some logs for debugging, like `console.log(self.getJson);` right before the critical line of code. – basilikum Jun 21 '14 at 09:08
  • I really don't see the connection to the "duplicate" question. – basilikum Jun 21 '14 at 09:12
  • Thank you for the response, yes it is on the exact line of the code. You are right, the "duplicate" question is about something else,I've voted for reopening, – DropDropped Jun 21 '14 at 09:13
  • 1
    I think I've just found the problem. The function loadData is initialized before getJson function, so I had to reorder these functions to make it work. Thank you all for your responses. – DropDropped Jun 21 '14 at 09:15

1 Answers1

0

you should try it like this

self.loadData = ko.computed(function(){
    var json = self.getJson()
    if(json){
        var data = new Data(json.data);
    }
});
Muhammad Raheel
  • 19,823
  • 7
  • 67
  • 103