Azamat Ism Esm
Azamat Ism Esm

Reputation: 15

knockout observable in ajax request

I have form which populates depend on current user id, however I need to add functionality which allows user to fill form on behalf another person. My ajax request look like this:

$.ajax({
url: "sharepoint/restapi/items?$filter=login eq '" + viewModel.userId() + "'&$select=city,directorate,person,department,desk,extension",
method: "GET",
headers: { "Accept": "application/json; odata=verbose" },
success: function (data) {
    if (data.d.results.length > 0) {
            viewModel.person(value.person);
            ...
        })
    }
},
error: function (data) {
   alert("Error: " + data);
}
}).done(function () {
   console.log('Person information loaded');
});

I construct url string by concatenating endpoint url + observable (userId) + select parameters. How to update data whenever my userId() changes on view?

Upvotes: 0

Views: 197

Answers (1)

Olha Shumeliuk
Olha Shumeliuk

Reputation: 740

you can subscribe for userId changes and then call your ajax method when change occur:

viewModel.userId.subscribe(function(newUser){
   //call ajax method here to update 
});

Upvotes: 0

Related Questions