是否有可能将自定义字段添加到检索到客户端的breeze实体对象并在保存更改之前将其删除?我需要在网格中显示实体对象列表(Student),并且网格具有基于其平均值的自定义字段[Rating]。我在客户端计算的评级。
更新: 我尝试通过覆盖实体构造函数,如下所示:
function overrideStudentEntity(metadataStore) {
metadataStore.registerEntityTypeCtor('Student', Student);
function Student() {
this.rating = 0;
}
}
答案 0 :(得分:0)
是的,您可以在客户端实体上添加自定义字段
例如我正在从student对象的firstName和lastName添加fullName
var myQuery = breeze.EntityQuery.from(' Student');
return manager.executeQuery(myQuery).then(function(studentData){ studentData.results.fullName = ko.observable(); studentData.results.fullName(studentData.results.FirstName()+'' + studentData.results.LastName()) })
现在无论何时触发manager.savechanges()方法,它都会自动保存学生对象。
答案 1 :(得分:0)
尝试覆盖默认的JSON适配器,就像在GitHub的无服务器示例中的“Edmunds”示例中一样:
https://github.com/Breeze/breeze.js.samples/blob/master/no-server/edmunds/app/jsonResultsAdapter.js
http://www.breezejs.com/sites/all/apidocs/classes/JsonResultsAdapter.html
app.value('jsonResultsAdapter',
new breeze.JsonResultsAdapter({
name: "edmunds",
extractResults: function(data) {
console.log(data);
var results = data.results;
if (!results) throw new Error("Unable to resolve 'results' property");
return results && (results.operation.details);
},
visitNode: function(node, parseContext, nodeContext) {
// Make parser
if (node.isoverdue) {
// move 'node.models' links so 'models' can be empty array
// node.modelLinks = node.models;
// node.models = [];
return {
entityType: "request"
}
}
// Model parser
else if (node.description) {
// move 'node.make' link so 'make' can be null reference
node.makeLink = node.make;
node.make = null;
// flatten styles and sizes as comma-separated strings
var styles = node.categories && node.categories["Vehicle Style"];
node.vehicleStyles = styles && styles.join(", ");
var sizes = node.categories && node.categories["Vehicle Size"];
node.vehicleSizes = sizes && sizes.join(", ");
return {
entityType: "requestDetails"
};
}
}
}));