作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我只是想从 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>
<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>
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>
<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/
我是一名优秀的程序员,十分优秀!