I have read allot about function expressions vs declaration, callbacks, hoisting, and I get the general idea of most of this but I guess I can’t quite grasp the concept because of the below code, let me post the code and then ask the real questions.
var url = "beverages.txt";
var GridModel = function () {
this.items = ko.observableArray();
var me = this;
$.ajax({
datatype: 'json',
url: "beverages.txt"
}).done(function (data) {
debugger;
var jsonData = $.parseJSON(data);
me.items(jsonData);
});
};
var model = new GridModel();
// prepare the data
var source =
{
datatype: "observablearray",
datafields: [
{ name: 'name' },
{ name: 'type' },
{ name: 'calories', type: 'int' },
{ name: 'totalfat' },
{ name: 'protein' },
],
id: 'id',
localdata: model.items
};
var dataAdapter = new $.jqx.dataAdapter(source);
$("#grid").jqxGrid(
{
width: 670,
source: dataAdapter,
theme: 'classic',
columns: [
{ text: 'Name', datafield: 'name', width: 250 },
{ text: 'Beverage Type', datafield: 'type', width: 250 },
{ text: 'Calories', datafield: 'calories', width: 180 },
{ text: 'Total Fat', datafield: 'totalfat', width: 120 },
{ text: 'Protein', datafield: 'protein', minwidth: 120 }
]
});
ko.applyBindings(model);
});
Ok so this code works fine, it calls the ajax request by var model = new GridModel();. The problem is that if I add a debugger; statement after var model = new GridModel(); it fails. Also the debugger statements inside of the ajax request don’t fire, however if I remove the debugger statement after the var model = new GridModel(); then ajax fires and I can debug the request. Why does it fail with the additional debugger, is it because var GridModel is an Expression.
Basically want I would like to do is is create a declaration function that I can call and when ajax request is done I return the observableArray me. If I change the function like this
function GridModel (param1,param2) {
this.items = ko.observableArray();
var me = this;
$.ajax({
datatype: 'json',
url: "beverages.txt"
}).done(function (data) {
debugger;
var jsonData = $.parseJSON(data);
me.items(jsonData);
});
return me
};
Then I would like to just be able to call that function like this var myitems = GridModel(param1,param2) with the expectation that myitems will now hold the results of the ajax request. I just don’t fully understand how the code execution flow works, If someone could explain why the bottom function doesn’t work and how to get it to work I would appreciate it.
Thanks,
Dan
If you have an asynchronous operation (like an Ajax request), and everything else depends on its result, resume your program’s flow from the callback. You can’t use
returnstatements, it only works for synchronous code.You may want to modify your
GridModelconstructor to take a callback as a parameter:Then resume your program flow from inside the callback:
And instantiate
GridModellike this: