1

I'm having the following ViewModel in which I have few functions. And I am trying to call another function located in the same ViewModel.

This is my ViewModel:

var UploadFileListVewModel = function() {
var self = this;

Inside I have an init function:

//initialize view model
this.init = function () {
    $.ajax({
        url: '/Files/LastUploadedFiles',
        type: 'POST',
        cache: false,
        data: {},
    }).done(function (result) {

        //doing stuff 
    });
}   

Sometime later on I perform an remove file operation and once it completed I want to run my init fuction again

self.removeFile = function(item)
{
    $('#fileModal').modal('hide'); //closing modal dialog       

    $.ajax({
        //url: '@Url.Action("RemoveFile", "Files")',
        url: '/Files/RemoveFile',
        type: 'POST',
        data: { 'file_id': self.fileToRemoveId() },
        cache: false
    }).done(function (result) {

    //doing stuff 
    this.init();

    });     
}

I'm getting an error this.init() is not a function.

I would really appreciate if somebody could point me into right direction on how to call this init() function or suggest any workaround.

1
  • @Buzinas, this did the trick!!! Thank you. I cannot believe I spent half day trying to figure it out.
    – Dmitri S.
    Commented Oct 7, 2015 at 1:21

1 Answer 1

2

The this keyword inside the callback function is not your ViewModel. That's why you created the self, so you can use it inside events, arrays, callback functions etc.

So, you should change your code to self.init().

1
  • 1
    Thank you for the explanation. It all works great now.
    – Dmitri S.
    Commented Oct 7, 2015 at 1:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.