所以我有这个:
function appViewModelRecord() {
var self = this;
self.returnedDataRecord = ko.observableArray([]);
self.table_name = ko.observable();
self.counta = ko.observable();
self.countb = ko.observable();
self.diff = ko.observable();
//
self.update = function() {
$.getJSON("@Url.HttpRouteUrl("ActionApi",new {controller="check",action="recordcount"})", function (data) {
$('#heartRecords').fadeTo(400, 1);
var mappedData = $.map(data, function (item) {
item.diff = item.counta - item.countb;
return new recordData(item);
});
self.returnedDataRecord(mappedData);
$('.badge').html(moment().format('DD/MM/YYYY HH:mm:ss'));
$('#heartRecords').fadeTo(400, 0);
});
});
};
}
我正在使用它:
var viewModelRecord = new appViewModelRecord();
updateData();
window.setInterval(updateData, 30000);
ko.applyBindings(viewModelRecord,document.getElementById('tableRecords'));
//
function updateData() {
viewModelRecord.update();
}
我希望在每次更新时检查returnedDataRecord arrat是否有0条记录,以便有可视指示。
我应该怎么做?更新
视图模型:
function appViewModelRecord() {
var self = this;
self.returnedDataRecord = ko.observableArray([]);
self.table_name = ko.observable();
self.counta = ko.observable();
self.countb = ko.observable();
self.diff = ko.observable();
//
self.update = function() {
$.getJSON("@Url.HttpRouteUrl("ActionApi",new {controller="check",action="recordcount"})", function (data) {
$('#heartRecords').fadeTo(400, 1);
var mappedData = $.map(data, function (item) {
item.diff = item.counta - item.countb;
return new recordData(item);
});
self.returnedDataRecord(mappedData);
$('.badge').html(moment().format('DD/MM/YYYY HH:mm:ss'));
$('#heartRecords').fadeTo(400, 0);
});
};
self.howMany = ko.computed(function () {
return self.returnedDataRecord().length;
});
}
和更新功能:
function updateData() {
viewModelRecord.update();
if (viewModelRecord.howMany() == 0) {
$('#noRecords').fadeTo(400, 1);
} else {
$('#noRecords').fadeTo(400, 0);
}
}
我有“记录”数。唯一的问题是它第一次有值= 0。
UPDATE2:
按照上面的代码,这是我的主要脚本:
var viewModel = new appViewModel();
var viewModelRecord = new appViewModelRecord();
ko.applyBindings(viewModel,document.getElementById('tablePallets'));
ko.applyBindings(viewModelRecord, document.getElementById('tableRecords'));
window.setInterval(updateData, 30000);
updateData();
就是这样,我第一次得不到计数
答案 0 :(得分:2)
<span data-bind="text: count"></span>
<button data-bind="click: addItem">Add Item</button>
function MyViewModel() {
var self = this;
self.items = ko.observableArray();
self.count = ko.computed(function() {
return self.items().length;
});
self.addItem = function(){
self.items.push("item");
}
}
ko.applyBindings(new MyViewModel());
答案 1 :(得分:1)
self.howMany = ko.computed(function () {
return self.returnedDataRecord().length;
});