代码之家  ›  专栏  ›  技术社区  ›  Tim Hutchison

用isDirty扩展属性

  •  0
  • Tim Hutchison  · 技术社区  · 9 年前

    因此,我只更新数据库中实际已更改的字段。目前,我使用的是一个数组,当属性发生变化时,我会在其中添加属性,然后遍历该数组,以确定数据库中哪些字段发生了变化,需要更新。然而,我更愿意通过某种isDirty检查来实现这一点。我的想法是我可以打电话给 if (property.dirty) then {} 确定属性是否已更改。

    我记得我能在

    当前代码

    class test{
      private _ID: Guid;
      private _dirty: Array<{}>;
    
      get ID(): Guid {
        return this._ID;
      }
      set ID(id: Guid) {
        if (this._ID != id) {
            this._ID = id;
            this._dirty.filter(function (f) { return f.Field == "id" }).length > 0 ? this._dirty.filter(function (f) { return f.Field == "id" })[0].Value = id.toString() : this._dirty.push({Field: "id", Value: id});
        }
      }
    
      get Name(): string {
          return this._Name;
      }
      set Name(name: string) {
          if (this._Name != name) {
              this._Name = name;
              this._DirtyFields.filter(function (f) { return f.Field == "ccseq_name" }).length > 0 ? this._DirtyFields.filter(function (f) { return f.Field == "ccseq_name" })[0].Value = name : this._DirtyFields.push(new EntityField("ccseq_name", name, FieldType.String));
          }
      }
    }
    

    所需代码

    class test{
      private _ID: Guid;
    
      get ID(): Guid {
        return this._ID;
      }
      set ID(id: Guid) {
        if (this._ID != id) {
            this._ID = id;
            this._ID.isDirty = true;
        }
      }
    
      get Name(): string {
          return this._Name;
      }
      set Name(name: string) {
          if (this._Name != name) {
              this._Name = name;
              this._Name.isDirty = true;
          }
      }
    }
    
    2 回复  |  直到 9 年前
        1
  •  0
  •   Nitzan Tomer    9 年前

    在javascript中,您可以向对象添加属性,因此这样做没有问题:

    this._ID.dirty = true;
    

    Guid 没有这个 dirty 成员

    为了避免这种情况,您可以简单地执行以下操作:

    private _ID: Guid & { dirty?: boolean };
    

    编辑

    同样,javascript已经支持它了,您可以这样做:

    obj.dirty = true;
    


    interface Object {
        dirty?: boolean;
    }
    

    但是请注意,您正在将其添加到代码中的**所有*对象中。由于您实际上没有更改原型,因此在运行时不会产生任何影响,但在类型脚本方面,它将影响所有实例。

        2
  •  0
  •   Tim Hutchison    8 年前

    Field类

    export class EntityField {
        private _Field: string;
        private _Value: any;
        private _FType: FieldType;
        private _isDirty: boolean;
    
        constructor(field: string, value: any, fType: FieldType) {
            this._Field = field;
            this._Value = value;
            this._FType = fType;
            this._isDirty = false;
        }
    
        markClean(): void {
            this._isDirty = false;
        }
    
        markDirty(): void {
            this._isDirty = true;
        }
    
        get isDirty(): boolean {
            return this._isDirty;
        }
    
        get Field(): string {
            return this._Field;
        }
        set Field(field) {
            if (this._Field !== field) {
                this._Field = field;
            }
        }
    
        get Value(): any {
            return this._Value;
        }
        set Value(value: any) {
            if (this._Value !== value) {
                this._Value = value;
                this._isDirty = true;
            }
        }
    
        get FType(): FieldType {
            return this._FType;
        }
        set FType(fType: FieldType) {
            if (this._FType != fType) {
                this._FType = fType;
            }
        }
    }
    

    用法

    export class Entity{
      public Name: Field
    }
    
    Entity test = new Entity()
    Entity.Name.isDirty() // Returns False
    Entity.Name.Value = "Test";
    Entity.Name.isDirty() // Returns True