问题描述:

This works fine:

var vm = {};

$.getJSON('file.json', function (data) {

vm.objects = data;

});

ko.applyBindings(vm);

But this doesn't work:

var vm = {

objects: $.getJSON('file.json', function (data) {});

};

ko.applyBindings(vm);

Why not?

Thanks

网友答案:

Because $.getJSON doesn't return anything (or, at least, it doesn't return your data). It's asynchronous, so the only way to access the data is in the callback function.

相关阅读:
Top