我有2个entites的1:M关系。
Vehicle (Id, RetailPrice, StockOn, VehicleInfoId) and
VehicleInfo (Id, Year, Make, Model, ICollection<Vehicle>)
这两个ID是用户指定的(串),而是因为微风需要独特的键(哪怕是暂时的)我要创建与实体id = breeze.core.getUuid()
此外,年份和RetailPrice被设置为0(年份是int,RetailPrice是十进制)。
有一次,我结合这些实体的“新车形式”这些值immediatelly显示给用户的时候,其实我想他们是空白...什么是处理的最佳方式? 有些想法,我有如下:
- 添加额外的性能(通过初始值设定),并使用这些在表格上结合。 然后保存之前,交换性能。
- “消灭”这些在初始化(不是构造函数)特性....
- 使用自定义KO结合处理可能?
有没有更好的办法?
这里是我到目前为止(使用#2,但我不知道这是正确的)
/viewmodels/vehicleadd.js
var vehicle = ko.observable();
var viewModel = {
title: 'Add New Vehicle',
vehicle: vehicle,
};
return viewModel;
function activate() {
vehicle(datacontext.createVehicle());
}
/services/datacontext.js
function init() {
logger.debug(datacontext, 'Initializing data context...');
return manager.fetchMetadata()
.then(model.configureMetadataStore(manager.metadataStore))
.then(model.applyValidators)
//.then(loadLookups)
.fail(initFailed);
}
function createVehicle() {
var newVehicleInfo = manager.createEntity('VehicleInfo');
var newVehicle = manager.createEntity('Vehicle');
newVehicle.vehicleInfo(newVehicleInfo);
return newVehicle;
}
/services/model.js
function configureMetadataStore(metadataStore) {
logger.debug(model, 'Configuring metadata store...');
extendVehicle(metadataStore);
extendVehicleInfo(metadataStore);
}
function extendVehicle(metadataStore) {
// --- custom constructor ---
var vehicleCtor = function () {
this.id = ko.observable(breeze.core.getUuid()); // must have for Breeze to work...
};
// --- initializer ---
var vehicleInitializer = function (vehicle) {
// defaults
vehicle.id(null); // user to input
vehicle.retailPrice(null); // user to input
vehicle.stockedOn(moment().format()); // set to current date & time
// additional, non persisted properties
vehicle.isBeingEdited = ko.observable(false);
vehicle.isBeingSaved = ko.observable(vehicle.entityAspect.isBeingSaved);
};
// --- register custom ctor & initializer ---
metadataStore.registerEntityTypeCtor(EntityNames.Vehicle, vehicleCtor, vehicleInitializer);
}
function extendVehicleInfo(metadataStore) {
// --- custom constructor ---
var vehicleInfoCtor = function () {
this.id = ko.observable(breeze.core.getUuid());
};
// --- initializer ---
var vehicleInfoInitializer = function (vehicleInfo) {
// defaults
vehicleInfo.year = ko.observable(moment().format());
// additional, non persisted properties
vehicleInfo.isBeingEdited = ko.observable(false);
vehicleInfo.isBeingSaved = ko.observable(vehicleInfo.entityAspect.isBeingSaved);
// computed properties
vehicleInfo.Description = ko.computed(function () {
return vehicleInfo.year() + ' ' + vehicleInfo.make() + ' ' + vehicleInfo.model();
});
};
// --- register custom ctor & initializer ---
metadataStore.registerEntityTypeCtor(EntityNames.VehicleInfo, vehicleInfoCtor, vehicleInfoInitializer);
}
更新#1
经过一些测试,看起来像2号查询时将无法正常工作。 之后(再)阅读“ 扩展实体 ”(DOC寻找“实体创建序列”段落),它专门说初始化函数是在两种情况下调用,创建和查询的情况下,实体时。 因此,“消灭”的ID,RetailPrice在初始化符查询和导入....