FlowContext

Trong step handler của registerFlow, tham số ctx chính là một instance FlowRuntimeContext. Thông qua chuỗi delegate, nó có thể truy cập tất cả các thuộc tính và phương thức ở cấp model và cấp engine.

Chuỗi delegate là:

FlowRuntimeContext (ngữ cảnh runtime của flow hiện tại)
  → FlowModelContext (model.context, cấp model)
    → FlowEngineContext (engine.context, cấp toàn cục)

Thuộc tính thường dùng

Các thuộc tính ctx được dùng nhiều nhất trong phát triển Plugin:

Thuộc tínhKiểuMô tả
ctx.modelFlowModelInstance FlowModel hiện tại
ctx.apiAPIClientClient HTTP request, đến từ @nocobase/sdk
ctx.viewerFlowViewerTrình quản lý dialog/drawer, cung cấp các phương thức dialog(), drawer()
ctx.messageMessageInstanceInstance message của Antd, ví dụ ctx.message.success('OK')
ctx.notificationNotificationInstanceInstance notification của Antd
ctx.modalHookAPIInstance Modal.useModal của Antd
ctx.t(key, options?)(string, object?) => stringPhương thức dịch i18n
ctx.routerRouterInstance router của react-router
ctx.routeRouteOptionsThông tin route hiện tại (observable)
ctx.locationLocationĐối tượng location của URL hiện tại (observable)
ctx.refReact.RefObjectDOM ref của container view của model hiện tại
ctx.flowKeystringKey của flow hiện tại
ctx.mode'runtime' | 'settings'Chế độ thực thi hiện tại, runtime là runtime, settings là panel cấu hình
ctx.tokenstringToken xác thực của người dùng hiện tại
ctx.rolestringVai trò của người dùng hiện tại
ctx.authobjectThông tin xác thực: { roleName, locale, token, user }
ctx.themeTokenobjectToken theme của Antd, dùng để lấy màu chủ đề
ctx.dataSourceManagerDataSourceManagerTrình quản lý nguồn dữ liệu
ctx.engineFlowEngineInstance FlowEngine
ctx.appApplicationInstance Application của NocoBase
ctx.i18ni18nInstance i18next

Phương thức thường dùng

Liên quan đến request

Phương thứcMô tả
ctx.request(options)Gửi HTTP request, URL nội bộ qua APIClient, URL bên ngoài qua axios
ctx.makeResource(ResourceClass)Tạo một instance Resource (ví dụ MultiRecordResource, SingleRecordResource)
ctx.initResource(className)Khởi tạo một resource trên model context

Liên quan đến popup

Phương thứcMô tả
ctx.viewer.dialog(options)Mở dialog, options.content nhận (view) => JSX, dùng view.close() để đóng
ctx.viewer.drawer(options)Mở drawer
ctx.openView(uid, options)Mở view đã đăng ký (popup / drawer / dialog)

Kiểm soát thực thi Flow

Phương thứcMô tả
ctx.exit()Ngắt thực thi flow hiện tại
ctx.exitAll()Ngắt thực thi tất cả flow
ctx.getStepParams(stepKey)Lấy tham số đã lưu của step chỉ định
ctx.setStepParams(stepKey, params)Đặt tham số cho step chỉ định
ctx.getStepResults(stepKey)Lấy kết quả thực thi của step trước đó

Action và Event

Phương thứcMô tả
ctx.runAction(actionName, params?)Thực thi một action đã đăng ký
ctx.getAction(name)Lấy định nghĩa action đã đăng ký
ctx.getActions()Lấy tất cả action đã đăng ký
ctx.getEvents()Lấy tất cả event đã đăng ký

Quyền

Phương thứcMô tả
ctx.aclCheck(params)Kiểm tra quyền ACL
ctx.aclInstance ACL

Khác

Phương thứcMô tả
ctx.resolveJsonTemplate(template)Phân giải template biểu thức {{ ctx.xxx }}
ctx.getVar(path)Phân giải đường dẫn biểu thức ctx.xxx.yyy
ctx.runjs(code, variables?, options?)Thực thi mã JavaScript động
ctx.requireAsync(url)Tải module động (kiểu CommonJS)
ctx.importAsync(url)Tải module động (kiểu ESM)
ctx.loadCSS(href)Tải file CSS động
ctx.onRefReady(ref, callback, timeout)Chờ React ref sẵn sàng rồi thực thi callback
ctx.defineProperty(key, options)Đăng ký thuộc tính mới động
ctx.defineMethod(name, fn, info?)Đăng ký phương thức mới động

Cách dùng điển hình trong phát triển Plugin

Hiển thị thông báo trong click handler

MyModel.registerFlow({
  key: 'clickFlow',
  on: 'click',
  steps: {
    showMessage: {
      async handler(ctx) {
        ctx.message.success(ctx.t('Thao tác thành công'));
      },
    },
  },
});

Mở popup tạo bản ghi

MyModel.registerFlow({
  key: 'clickFlow',
  on: 'click',
  steps: {
    openDialog: {
      async handler(ctx) {
        ctx.viewer.dialog({
          title: ctx.t('Tạo bản ghi mới'),
          content: (view) => <MyForm onClose={() => view.close()} />,
        });
      },
    },
  },
});

Lấy dữ liệu hàng hiện tại (thao tác cấp bản ghi)

MyRecordAction.registerFlow({
  key: 'clickFlow',
  on: 'click',
  steps: {
    showRecord: {
      async handler(ctx) {
        const index = ctx.model.context.recordIndex;
        const record = ctx.model.context.record;
        ctx.message.info(`Hàng ${index}: ${record.title}`);
      },
    },
  },
});

Thao tác dữ liệu qua resource

async handler(ctx) {
  const resource = ctx.model.context.resource;
  // Tạo bản ghi
  await resource.create({ title: 'New item', completed: false });
  // Refresh dữ liệu
  await resource.refresh();
}

Liên kết liên quan