KoGrid JSON动态小部件,带有嵌套服务器调用 [英] KoGrid JSON Dynamic widgets, with nested server calls
问题描述
我正在使用KOGrid的仪表板上工作,我的想法是动态创建一堆小部件并将每个小部件的数据绑定到kogrid中.我有两个控制器(MVC4),其中第一个拳头返回一个小部件名称列表,第二个控制器以JSON格式返回每个小部件的结果(数据表).我正在使用JSON.Net JsonConvert将结果转换为json格式,然后作为JSON内容结果传递.
I am working on a dashboard where I am using KOGrid, my idea is to dynamically create a bunch of widgets and bind data into the kogrid for each widget. I have two controllers (MVC4), where the fist returns a list of widget names and the second returns results (datatable) for each widget in JSON format. I am using JSON.Net JsonConvert to convert the results to json format then passing as JSON content result.
我收到未捕获的错误:初始化可观察数组时传递的参数必须是数组,或者为null或未定义". 嵌套的JSON是否有问题?处理复杂对象数组并将它们绑定到多个网格的最可靠方法是什么.如果我使用基因敲除会有所帮助吗?
I am getting "Uncaught Error: The argument passed when initializing an observable array must be an array, or null, or undefined". Is it the nested JSON calls a problem? What is the most reliable way to handle Arrays of complex objects and binding them to multiple grids. Does it help if I use knockout.mapping?
var widgets = {};
var widgets = {};
var Widget = function (id, data) {
this.id = ko.observableArray(id || []);
this.data = ko.observableArray(data || []);
};
var ViewModel = function (data) {
var self = this;
self.widgets = ko.observableArray(
ko.utils.arrayMap(data, function (i) {
return new Widget(i);
}));
$.getJSON('Widgets/Get', null, function (data) {
$.each(data, function (index, item) {
$.getJSON('Home/GetWidgetDetails?widgetName=' + item.WIDGET_NAME,
function (result) {
self.widgets.push(new Widget({ id: item.WIDGET_NAME, data: result }));
});
});
});
};
ko.applyBindings(new ViewModel(widgets));
推荐答案
我猜您在Widget
类的data
道具上遇到此错误?如果是这样,则可能是data
数组的JSON结构.如果它们本身是复杂的对象,则应重复与小部件相同的过程.
I'm guessing you are getting this error on the data
prop in the Widget
class? If so, then yes it is probably the JSON structure of the data
array. If they are complex objects themselves, you should repeat the same process you did for Widgets.
- 为这些项目创建对象定义.也许
WidgetItem
. - 使用
ko.utils.arrayMap
将data
数组映射到WidgetItem
. - 成功.
- Make an object definition for these items. Perhaps
WidgetItem
. - use
ko.utils.arrayMap
to map thedata
array ontoWidgetItem
. - Success.
尽管没有看到data
的结构,但我无法提供更具体的信息.
Without seeing the structure of data
though, I can't give you anything more specific.
这篇关于KoGrid JSON动态小部件,带有嵌套服务器调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!