I'm using Knockout JS and Durandal JS. I want to query the database from a separate Js file and bind the data in the viewModel Js file.
I'm having a index.js file and contains a listData as an observableArray which is bound to the view(index.html). In index.js i'm calling a function to get data form another file:apiCall.js. In apiCall.js i'm calling a getJson method to get a list from database. Now, how should i bind the new list to the listData observableArray? Since Ajax works Asynchronously, all other code gets executed before getJson call. Also, return won't work as expected.
How am I supposed to do it? If I call getJson or ajax form the same viewModel file itself(index.js), it works fine because we can push data easily. Is it goot to have a separate file for only ajax calls?
index.js
var activate = function () {
var self = this;
self.listData(apiCall.listData());
...}
apiCall.js
var listData = function () {
$.getJSON("/api/Clients", function (data) {
return data;
}
}