Any built-in ready-to-use solution in Kendo UI to parse JSON data according to schema.model
? Maybe something like kendo.parseData(json, model)
, which will return array of objects?
任何内置的即用型解决方案,在Kendo UI中根据schema.model解析JSON数据?也许像kendo.parseData(json,model)这样会返回对象数组?
2 个解决方案
#1
I was searching for something like that and couldn't find anything built-in. However, using Model.set
apparently uses each field's parse logic, so I ended up writing this function which works pretty good:
我正在寻找类似的东西,找不到任何内置的东西。但是,使用Model.set显然使用了每个字段的解析逻辑,所以我最终编写了这个非常好用的函数:
function parse(model, json) {
// I initialize the model with the json data as a quick fix since
// setting the id field doesn't seem to work.
var parsed = new model(json);
var fields = Object.keys(model.fields);
for (var i=0; i<fields.length; i++) {
parsed.set(fields[i], json[fields[i]]);
}
return parsed;
}
Where model
is the kendo.data.Model
definition (or simply datasource.schema.model
), and json
is the raw object. Using or modifying it to accept and return arrays shouldn't be too hard, but for my use case I only needed a single object to be parsed at a time.
其中model是kendo.data.Model定义(或简称datasource.schema.model),json是原始对象。使用或修改它来接受和返回数组应该不会太难,但对于我的用例,我一次只需要解析一个对象。
#2
I actually saw your post the day you posted it but did not have the answer. I just needed to solve this problem myself as part of a refactoring. My solution is for DataSources, not for models directly.
我实际上在你发布它的那天看到了你的帖子,但没有答案。作为重构的一部分,我只需要自己解决这个问题。我的解决方案是DataSources,而不是直接用于模型。
kendo.data.DataSource.prototype.parse = function (data) {
return this.reader.data(data);
// Note that the original data will be modified. If that is not what you want, change to the following commented line
// return this.reader.data($.extend({}, data));
}
// ...
someGrid.dataSource.parse(myData);
If you want to do it directly with a model, you will need to look at the DataReader class in kendo.data.js and use a similar logic. Unfortunately, the DataReader takes a schema instead of a model and the part dealing with the model is not extracted in it's own method.
如果要直接使用模型,则需要查看kendo.data.js中的DataReader类并使用类似的逻辑。不幸的是,DataReader采用模式而不是模型,并且处理模型的部分不是在它自己的方法中提取的。
#1
I was searching for something like that and couldn't find anything built-in. However, using Model.set
apparently uses each field's parse logic, so I ended up writing this function which works pretty good:
我正在寻找类似的东西,找不到任何内置的东西。但是,使用Model.set显然使用了每个字段的解析逻辑,所以我最终编写了这个非常好用的函数:
function parse(model, json) {
// I initialize the model with the json data as a quick fix since
// setting the id field doesn't seem to work.
var parsed = new model(json);
var fields = Object.keys(model.fields);
for (var i=0; i<fields.length; i++) {
parsed.set(fields[i], json[fields[i]]);
}
return parsed;
}
Where model
is the kendo.data.Model
definition (or simply datasource.schema.model
), and json
is the raw object. Using or modifying it to accept and return arrays shouldn't be too hard, but for my use case I only needed a single object to be parsed at a time.
其中model是kendo.data.Model定义(或简称datasource.schema.model),json是原始对象。使用或修改它来接受和返回数组应该不会太难,但对于我的用例,我一次只需要解析一个对象。
#2
I actually saw your post the day you posted it but did not have the answer. I just needed to solve this problem myself as part of a refactoring. My solution is for DataSources, not for models directly.
我实际上在你发布它的那天看到了你的帖子,但没有答案。作为重构的一部分,我只需要自己解决这个问题。我的解决方案是DataSources,而不是直接用于模型。
kendo.data.DataSource.prototype.parse = function (data) {
return this.reader.data(data);
// Note that the original data will be modified. If that is not what you want, change to the following commented line
// return this.reader.data($.extend({}, data));
}
// ...
someGrid.dataSource.parse(myData);
If you want to do it directly with a model, you will need to look at the DataReader class in kendo.data.js and use a similar logic. Unfortunately, the DataReader takes a schema instead of a model and the part dealing with the model is not extracted in it's own method.
如果要直接使用模型,则需要查看kendo.data.js中的DataReader类并使用类似的逻辑。不幸的是,DataReader采用模式而不是模型,并且处理模型的部分不是在它自己的方法中提取的。