【问题标题】:Generate metadata from interface breeze+Typescript从界面brew+Typescript生成元数据
【发布时间】:2015-03-17 06:50:43
【问题描述】:

我有一些模型,我想在打字稿中使用它们,以便微风可以使用这些模型并构建metadataStore,但不知道该怎么做,有一些方法可以手动添加属性,但是我想从现有模型构建metadataStore

这是我由 T4TS 生成的 typescript 接口

/** Generated from Twine.Infrastructure.Entity **/
export interface IEntity extends breeze.Entity {
  _id: number;
  _created_at?: string;
  _updated_at?: string;
}
/** Generated from Twine.Infrastructure.Models.Ingredient **/
export interface IIngredient extends twine.Model.IEntity {
  Price?: number;
  Name: string;
  Description: string;
}

这个extends breeze.Entity确实是我加的。

这是应该从T 获取元数据并通过 id 返回单个结果的服务

Get<T>( key:string,  successCallback: Function, failCallback?: Function ): void {
  var type = new breeze.EntityType(); //what should be here???
  var entityKey = new breeze.EntityKey(type, key);
  this.manager.fetchEntityByKey(entityKey)
    .then( ( data: breeze.EntityByKeyResult ) => { successCallback( data ) })
    .catch( ( reason: any ) => {
      if ( failCallback ) {
        failCallback( reason );
      }
    });
}

所以new breeze.EntityType();这个构造函数接受EntityTypeOptions,看起来像

interface EntityTypeOptions {
    shortName?: string;
    namespace?: string;
    autoGeneratedKeyType?: AutoGeneratedKeyType;
    defaultResourceName?: string;
    dataProperties?: DataProperty[];
    navigationProperties?: NavigationProperty[];
}

那么我在哪里添加我现有的IEntity 模型?这样我就不需要从 EntityTypeOptions 构建它们了

【问题讨论】:

    标签: typescript entity breeze


    【解决方案1】:

    你们的服务器技术是什么?您是否在服务器上使用实体框架?如果是这样,元数据将来自服务器。您可以通过 EFContextProvider 在 Web API 控制器中提取它,如下所示:

    http://www.getbreezenow.com/documentation/efcontextprovider

    然后在客户端上,您将为每个实体类型生成 TS 类,并使用 Breeze 注册 ctor。您可以在这个相关问题中找到一个示例。

    Extending a breeze entity using TypeScript

    仅生成接口不会让您走得太远,因为接口在 TS 中的运行时没有意义。它们仅在开发期间有用。

    【讨论】:

    猜你喜欢
    • 2010-12-26
    • 1970-01-01
    • 2020-02-13
    • 2010-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    相关资源
    最近更新 更多