跳转至

Use Foundry Functions in Slate(在 Slate 中使用 Foundry Functions)

The Foundry Functions panel allows Slate to execute business logic written in a Functions repository as a "backend" service. In this paradigm, consider the Slate application the client, the Foundry Functions implementation the server, and the Ontology the database of a traditional web application infrastructure. Consult the documentation for more information on how to get started with Foundry Functions or how to use Functions on Objects.

The Foundry Functions panel lets you:

  • Select a Foundry Function for which you have access,
  • Select the version of this Function (e.g. version 0.0.5),
  • Pass a typed Input into the Function, if the Function permits (note that if the Function takes object sets as inputs, you can pass in Slate-defined Object Sets as inputs via Handlebars and resolve them), and
  • Use the typed Output of the Function in Slate by referencing the Function’s handlebar {{ff_foundry_function1}}.

The Function will need to be created in the Function Repository before being used in Slate.

Approaches for Slate

When writing Functions to use in a Slate application, you should consider two approaches that differ based on where the application logic is implemented.

  • In a thin Functions approach, the Functions are a simple pass-through to access data in the Ontology API. Any additional manipulation, for example to scale a value appropriately to use as the Radius for a Scatterplot chart, is done in the Slate app in a regular JavaScript function.
  • In a thick Functions approach, all the business and display logic is also pushed into the Foundry Functions layer, leaving only very specific formatting or display logic and the actual layout and widget configuration within the Slate app itself.

Using Foundry Functions to organize and manage the logic layer of a complex Slate application has significant benefits, such as:

  • Strong typing, which is a best practice that prevents entire categories of potential edge cases.
  • Working in a repository, which provides separate versioning and collaboration for the frontend and backend of the application, and also enables standard practices for organizing and structuring code.
  • Decoupling application logic from a specific frontend creates reusable components that can be referenced in other applications.

Function Input and Output Types

Foundry Functions supports a wide range of standard, ontology-generated, and custom input and output types as documented in Functions Input and Output Types.

In a thick Functions approach, it's common to define custom input and output types so that data is returned in exactly the format necessary to populate specific charts or populate HTML widgets.


中文翻译

在 Slate 中使用 Foundry Functions

Foundry Functions 面板允许 Slate 将 Functions 仓库中编写的业务逻辑作为"后端"服务来执行。在这种模式下,可以将 Slate 应用视为传统 Web 应用架构中的客户端,Foundry Functions 实现视为服务器,而 Ontology 则视为数据库。有关如何开始使用 Foundry Functions 或如何使用 Functions on Objects 的更多信息,请查阅相关文档。

Foundry Functions 面板允许您:

  • 选择您有权访问的 Foundry Function,
  • 选择该 Function 的版本(例如版本 0.0.5),
  • 如果 Function 允许,向其传递类型化的输入(Input)(注意:如果 Function 接受对象集作为输入,您可以通过 Handlebars 传递 Slate 定义的对象集作为输入并进行解析),以及
  • 通过引用 Function 的 Handlebars 表达式 {{ff_foundry_function1}},在 Slate 中使用 Function 的类型化输出(Output)。

在 Slate 中使用 Function 之前,需要先在 Function Repository 中创建该 Function。

Slate 的实现方法

在编写用于 Slate 应用的 Functions 时,您应考虑两种方法,它们的区别在于应用逻辑的实现位置。

  • 薄 Functions 方法中,Functions 仅作为访问 Ontology API 数据的简单通道。任何额外的数据处理(例如,适当缩放数值以用作散点图Radius 参数)都在 Slate 应用中通过常规 JavaScript 函数完成。
  • 厚 Functions 方法中,所有业务逻辑和显示逻辑都被推送到 Foundry Functions 层,Slate 应用本身仅保留非常具体的格式化或显示逻辑,以及实际的布局和小部件配置。

使用 Foundry Functions 来组织和管理复杂 Slate 应用的逻辑层具有显著优势,例如:

  • 强类型(Strong typing),这是一项最佳实践,可以防止整个类别的潜在边界情况。
  • 在仓库中工作(Working in a repository),为应用的前端和后端提供独立的版本控制和协作功能,同时也支持代码组织和结构化的标准实践。
  • 将应用逻辑与特定前端解耦(Decoupling application logic from a specific frontend),创建可在其他应用中复用的组件。

Function 的输入和输出类型

Foundry Functions 支持多种标准类型、Ontology 生成类型以及自定义输入和输出类型,详见 Functions 输入和输出类型 文档。

厚 Functions 方法中,通常需要定义自定义输入和输出类型,以便数据以填充特定图表或 HTML 小部件所需的精确格式返回。