EntityManager.createEntity()-用于显示用户输入的生成ID和默认值的策略 [英] EntityManager.createEntity() - Strategies for showing generated ids and default values for user input

查看:84
本文介绍了EntityManager.createEntity()-用于显示用户输入的生成ID和默认值的策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个具有1:M关系的实体.

I have 2 entites with 1:M relationship.

Vehicle (Id, RetailPrice, StockOn, VehicleInfoId) and 
VehicleInfo (Id, Year, Make, Model, ICollection<Vehicle>)

两个Id都是用户指定的(字符串),但是因为Breeze需要唯一键(即使是临时键),所以我必须使用id = breeze.core.getUuid()创建实体. 此外,将Year和RetailPrice设置为0(Year是一个整数,RetailPrice是一个十进制).

Both Ids are user specified (strings) but because Breeze requires unique keys (even if temporary) I have to create entities with id = breeze.core.getUuid(). Furthermore, Year and RetailPrice are set to 0 (Year is an int, RetailPrice is decimal).

一旦我将这些实体绑定到新车辆表格",这些值就会立即显示给用户,而实际上我希望它们为空白... 处理此问题的最佳方法是什么? 我有一些想法:

Once I bind these entities to a "New Vehicle form" these values are immediatelly shown to the user, when in fact I want them to be blank... What is the best way to handle this? Some ideas I have are:

  1. 添加其他属性(通过初始值设定项),并使用这些属性在表单上进行绑定.然后在保存之前,交换属性.
  2. 在初始化程序(而不是构造函数)中擦除"这些属性....
  3. 也许使用自定义KO绑定处理程序?

有更好的方法吗?

这是我到目前为止(使用#2,但我不确定这是否正确)

Here is what I have so far (using #2, but I am not sure if this is right)

/viewmodels/vehicleadd.js

/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

/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

/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在查询时将无法工作. (重新)阅读"扩展实体"文档后(查找实体创建顺序"段落),它特别指出在创建实体和进行查询的两种情况下都调用初始化函数.因此,清除" Ids,在初始值设定项中的RetailPrice会中断查询和导入....

Update #1

After some testing, looks like #2 will not work when querying. After (re) reading the "Extending Entities" doc (look for "Entity creation sequence" paragraph), it specifically says that initializer function is invoked in both cases, when entity is created and in case of querying. So "wiping out" Ids, RetailPrice in initializer breaks querying and importing....

推荐答案

我会考虑添加在初始值设定项中计算的读/写敲除(KO).

I would look into adding a read/write Knockout (KO) computed in the initializer.

当值是GUID ...或您设计的任何其他唯一临时值时,计算值的读取"端将返回空白.

The "read" side of the computed would return blank when the value is a GUID ... or any other unique temporary value you devise.

写"侧将(a)确认高速缓存中没有其他实体具有该键,然后(b)用新值覆盖该键.现在,读取"侧将显示该值,因为它不再是临时".

The "write" side would (a) confirm that no other entity in cache had that key and then (b) overwrite the key with the new value. The "read" side would now display that value because it is no longer "temporary".

如果在任何其他关联中使用这些密钥,您将遭受很多痛苦;您将不得不手动修复它们……也许是在您的KO计算的写入逻辑中.

You're in a world of hurt if you are using these keys in any other associations; you'll have to fix them up manually ... perhaps inside your KO computed write logic.

我假设您已经为用户输入的值与其他用户输入的数据库中的另一个键发生冲突的可能性做好了准备.

I assume you are already prepared for the possibility that the user-entered value collides with another key on the database, entered by some other user.

我将为您腾出关于语义密钥的讲座(如不要使用它们"中的内容);我认为一些旧式数据库使您处于这一位置.

I will spare you the lecture on semantic keys (as in "don't use them"); I figure some legacy database put you in this spot.

这篇关于EntityManager.createEntity()-用于显示用户输入的生成ID和默认值的策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆