Knockout.js KnockoutJS-对ko.computed和AJAX请求的不满

Knockout.js KnockoutJS-对ko.computed和AJAX请求的不满,knockout.js,knockout-2.0,Knockout.js,Knockout 2.0,我只是想从ajax请求中提取一些数据。ajax调用有效——我知道数据已被检索。但它只是没有设置ko的值。计算 function viewModel() { this.Id = ko.observable("@Model.Identity()"); this.Points = ko.computed({ read: function () { $.ajax({

我只是想从ajax请求中提取一些数据。ajax调用有效——我知道数据已被检索。但它只是没有设置ko的值。计算

        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
            });
        }
所以一个像

<span data-bind="text: Points"></span>
然后尝试显示检索到的任何数据仍然失败

<span data-bind="text: User.Name"></span>

更新(3) 有点突破!如果我将声明性绑定更改为如下所示

<span data-bind="with: User">
    <span data-bind="text: Id"></span>
    <span data-bind="text: Name"></span>
</span>


然后我开始看到结果。我想我快到了……

敲除绑定不支持异步计算

相反,您应该使用常规属性并将其设置为AJAX请求的结果。
您可以通过在依赖属性的更改处理程序中重新发送AJAX请求来观察它


您还可以使用单独的占位符值向绑定的UI添加加载指示器。

正如SLaks所指出的,由于调用是异步进行的,因此无法以这种方式进行,即“read”函数在检索响应之前返回。我想推荐如下:

function viewModel() {
    var self = this;
    this.Id = ko.observable("@Model.Identity()");
    this.Points = ko.observable(0);

    var refreshPoints = function() {
        $.ajax({
            url: '/skills/points',
            data: { id: self.Id() }, // <-- you need () here!
            type: 'get',
            success: function (data) {
                self.Points(data);
            }
        });
    };

    // Now subscribe to the ID observable to update the points whenever the 
    // ID gets changed:
    this.Id.subscribe(refreshPoints);
}
函数viewModel(){
var self=这个;
this.Id=ko.observable(@Model.Identity());
该点=可观测的ko(0);
var refreshPoints=函数(){
$.ajax({
url:“/skills/points”,

数据:{id:self.id()},//只需将一个可观察变量绑定到html变量,并在ko.computed字段中更新该字段。不要直接将ko.computed字段绑定到html变量

self.htmlUserName = ko.observable();

self.computedUserName = ko.computed(function () {
    $.ajax(
    ....
    success: function (result) {
    self.htmlUserName(result);
    }
}

欢迎来到奇妙的异步世界!你不能这样做。那么我能做些什么来获得赋值呢?
function viewModel() {
    var self = this;
    this.Id = ko.observable("@Model.Identity()");
    this.Points = ko.observable(0);

    var refreshPoints = function() {
        $.ajax({
            url: '/skills/points',
            data: { id: self.Id() }, // <-- you need () here!
            type: 'get',
            success: function (data) {
                self.Points(data);
            }
        });
    };

    // Now subscribe to the ID observable to update the points whenever the 
    // ID gets changed:
    this.Id.subscribe(refreshPoints);
}
self.htmlUserName = ko.observable();

self.computedUserName = ko.computed(function () {
    $.ajax(
    ....
    success: function (result) {
    self.htmlUserName(result);
    }
}