14
I am simply trying to pull some data from an ajax request. The ajax call works - I know the data is retrieved. But it just isn't setting the value of the ko.computed...
function viewModel() {
this.Id = ko.observable("@Model.Identity()");
this.Points = ko.computed({
read: function () {
$.ajax({
url: '/skills/points',
data: { id: this.Id },
type: 'get',
success: function (data) {
return data;
}
});
},
owner: this
});
}
So a call like ...
<span data-bind="text: Points"></span>
Just isn't working. Can anyone help me figure out what I am doing so wrong?
Update
I'm using the following code, following RPN's suggestion - and I just can't get it to function at all. It won't look at the controller, it won't return the data... it just does nothing. I've tried all three examples given with no success.
<script type="text/javascript">
//an observable that retrieves its value when first bound
ko.onDemandObservable = function (callback, target) {
var _value = ko.observable(); //private observable
var result = ko.computed({
read: function () {
//if it has not been loaded, execute the supplied function
if (!result.loaded()) {
callback.call(target);
}
//always return the current value
return _value();
},
write: function (newValue) {
//indicate that the value is now loaded and set it
result.loaded(true);
_value(newValue);
},
deferEvaluation: true //d