我正在尝试将ViewModel绑定到Kendo DataSource,而Kendo DataSource又提供给Kendo Grid。没有什么太花哨的了。
它有点奏效,但非常慢!我有一个警告通知我,我在2秒内收到了我的json数据(700行),但是更新视图模型需要大约15秒。
我做错了什么?
由于
$(document).ready(function () {
// create the viewmodel we use as the source for the list
var viewModel = kendo.observable({
items: [],
total: function () {
return this.get("items").length;
}
});
var dataSource2 = new kendo.data.DataSource({
data: viewModel,
pageSize: 50
});
// create the grid
$("#grid").kendoGrid({
dataSource: dataSource2,
height: 500,
scrollable: {
virtual: true
},
columns: [
{ field: "ID_ORDER", title: "ID", width: 80 },
{ field: "CREATION_DATE", title: "Creation Date" },
{ field: "STATUS", title: "STATUS", width: 80 },
** more columns (around 10) **
]
});
// pass this on to initialise
APPS.View.Orders.Initialise(viewModel);
});
然后在我的打字稿中,我正在处理传入viewModel的Initialise调用:
module APP.View.Orders {
export var _Scope: string = "Orders";
var _viewModelOrders: any;
export var Initialise = function (viewModelOrders: any) {
_viewModelOrders = viewModelOrders;
var orderdetails = {
userid: APP.Core.userID,
context: "DEAL"
};
// retrieve all orders
$.getJSON("/api/omsapi/GetOrders", orderdetails, function (mydata) {
try {
alert("item count (1): " + mydata.length);
jQuery.each(mydata, function () {
var newItem = this;
_viewModelOrders.items.push(newItem);
});
alert("item count (2): " + _viewModelOrders.items.length);
}
catch (e) {
alert(e.message);
}
});
}
}
答案 0 :(得分:1)
尝试构建项目数组,然后将其分配到模型中。
类似的东西:
// retrieve all orders
$.getJSON("/api/omsapi/GetOrders", orderdetails, function (mydata) {
try {
alert("item count (1): " + mydata.length);
var items = [];
jQuery.each(mydata, function () {
items.push(this);
});
_viewModelOrders.items = items;
alert("item count (2): " + _viewModelOrders.items.length);
}
catch (e) {
alert(e.message);
}
});
答案 1 :(得分:1)
您可以通过执行以下操作暂时暂停observable:
$.getJSON("/api/omsapi/GetOrders", orderdetails, function (mydata) {
try {
var simpleArray = viewModel.items(); // get a reference to the underlying array instance of the observable
jQuery.each(mydata, function () {
items.push(this);
});
viewModel.items.valueHasMutated(); // let the observable know it's underlying data has been updated
}
catch (e) {
alert(e.message);
}
}
执行上述技术可显着缩短加载时间。我在合理的时间内测试了这几千行。
答案 2 :(得分:0)
进一步解释,这是由于行:
_viewModelOrders.items.push(newItem);
每次将项目推入数组时,都会触发change
事件,Grid会看到并自行更新。因此,如果您推送700个项目,那么您实际上正在使网格更新DOM 700次。
将所有项聚合到一个数组中会更好,然后将数组分配给DataSource,类似于:
$.getJSON("/api/omsapi/GetOrders", orderdetails, function (mydata) {
datasource2.data(mydata);