使用Breeze元数据帮助器如何手动构建客户端的继承

时间:2016-06-11 19:45:10

标签: javascript node.js inheritance mongoose breeze

使用 mongoose Node.js ,我创建了 Base 实体,该实体被分类为用户实体和一个 Person 实体,都有一个 Family 实体的引用,我的问题是使用 breeze-MetadataHelper 我如何定义一个family可以有 navigationProperties 数组,该数组包含 Person User 实体。总之,在失去OOP的情况下,有没有办法用Breeze在客户端上构建具有多态支持的继承,这就是我想出来的。

   var addFamilyItemType = (): any => {
            var entityType = {
                name: this.entityNames.Family,
                dataProperties: {
                    id: {type: DT.MongoObjectId},
                    name: {type: DT.String, required: true, max: 50}
                },

                navigationProperties: {
                //this is where i want one navigation since they are a subclass
                // members: {type: [], hasMany: true}
                    users: {type: [this.entityNames.User], hasMany: true},
                    people: {type: [this.entityNames.Person], hasMany: true}
                }
            };

            helper.addTypeToStore(store, entityType);
            return entityType;
        };

  var addUserItemType = (): any => {
            var entityType = {
                name: this.entityNames.User,
                dataProperties: {
                    id: {type: DT.MongoObjectId},
                    email: {type: DT.String, required: true, max: 50},
                    username: {type: DT.String, required: true, max: 20},
                    password: {type: DT.String, 'default': ''}
                },
                navigationProperties: {
                    family: {
                        entityTypeName: this.entityNames.Family,
                        associationName: 'User_Family',
                        foreignKeyNames: ['familyId']
                    }
                }
            };

            helper.addTypeToStore(store, entityType);
            return entityType;
        };

 var addPersonItemType = (): any => {
            var entityType = {
                name: this.entityNames.Person,
                dataProperties: {
                    id: {type: DT.MongoObjectId},
                    email: {type: DT.String, max: 50},
                    profileImageURL: {type: DT.String,'default':''}                       
                },
                navigationProperties: {
                    family: {
                        entityTypeName: this.entityNames.Family,
                        associationName: 'Person_Family',
                        foreignKeyNames: ['familyId']
                    }
                }
            };

            helper.addTypeToStore(store, entityType);
            return entityType;
        };

1 个答案:

答案 0 :(得分:0)

我找到的解决方案是将一个名为members的getter属性添加到FamilyModel对象中,该对象在调用registerEntityTypeCtor时将返回两个数组User和Person的并集,我认为breeze支持继承的开箱即用。

var metadataStore = this.metadata.createMetadataStore();
var registerType = metadataStore.registerEntityTypeCtor.bind(metadataStore);

registerType(this.metadata.entityNames.Family, FamilyModel);