gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 21:47:25 25 4
gpt4 key购买 nike

我只是想从 ajax 请求中提取一些数据。 ajax 调用有效 - 我知道数据已被检索。但它只是没有设置 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
});
}

所以像...这样的电话
<span data-bind="text: Points"></span>

只是不工作。谁能帮我弄清楚我做错了什么?

更新

我正在使用以下代码,遵循 RPN 的建议 - 我根本无法让它发挥作用。它不会查看 Controller ,也不会返回数据……它什么也不做。我已经尝试了所有三个示例,但均未成功。
<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 //do not evaluate immediately when created
});

//expose the current state, which can be bound against
result.loaded = ko.observable();
//load it again
result.refresh = function () {
result.loaded(false);
};

return result;
};

$(document).ready(function () {
function User(id, name) {
this.Id = ko.observable(id);
this.Name = ko.observable(name);
}
function viewModel() {
var self = this;

this.User = ko.onDemandObservable(this.Update, this);

this.Update = function () {
return $.ajax({
url: '/home/user/',
data: { id: 1 },
dataType: 'json'
}).pipe(function (data) {
return new User(data.Id, data.Name);
});
};
};
var model = new viewModel();
ko.applyBindings(model);
model.User();
});
</script>

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

更新 (2)

按照更多说明,我缩小了一些问题的范围。定义 callback作为 viewModel 上的函数似乎不起作用(我不确定为什么),但声明内联函数确实会产生......不同的东西。仍然无法正常工作。这是一个更新。
<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 //do not evaluate immediately when created
});

//expose the current state, which can be bound against
result.loaded = ko.observable();
//load it again
result.refresh = function () {
result.loaded(false);
};

return result;
};

$(document).ready(function () {
function User(id, name) {
this.Id = ko.observable(id);
this.Name = ko.observable(name);
}
function viewModel() {
var self = this;

this.User = ko.onDemandObservable(function(){
$.ajax({
url: '/home/user/',
data: { id: 1 },
dataType: 'json',
success: function (data) {
self.User(new User(data.Id, data.Name));
}
});
}, this);

//this.Update = function () {
// $.ajax({
// url: '/home/user/',
// data: { id: 1 },
// dataType: 'json',
// success: function (data) {
// self.User(new User(data.Id, data.Name));
// }
// });
//};
};
var model = new viewModel();
ko.applyBindings(model);
model.User();
});
</script>

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

更新 (3)

有点突破!如果我将声明性绑定(bind)更改为如下所示..
<span data-bind="with: User">
<span data-bind="text: Id"></span>
<span data-bind="text: Name"></span>
</span>

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

最佳答案

正如 SLaks 指出的那样,由于调用是异步进行的,因此您不能这样做,即“读取”函数在检索到响应之前返回。我会推荐这样的东西:

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);
}

关于knockout.js - KnockoutJS - 对 ko.computed 和 AJAX 请求的不满,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13783679/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com