东莞市连易网络科技有限公司
东莞市连易网络科技有限公司
  • 网站
  • 用户
  • 购物车
  • 购物车
  • 搜索
  • 网站
  • 用户
帮助
帮助
开发文档 小组件上下文
  • 用户文档
  • 开发文档
  • API文档
  • 提及
  • 标签
  • 更多
  • 取消
  • 新建
  • +开始
  • -UI 自定义
    • -UI 扩展
      • HTML 标头
      • HTTP 回调
      • Sockets
      • 出厂默认小组件提供程序
      • 小组件上下文
      • 小组件扩展
      • 扩展上下文管理 UI
      • 扩展内容编辑器
      • 扩展系统管理 UI
      • 页面定义和 URL 路由
    • UI 自动化
    • 主题
    • +小组件
  • +外部集成
  • +插件/框架扩展

小组件上下文

小组件定义它们需要渲染的上下文。例如,“博客 - 文章”小组件始终显示当前上下文博客文章。这取决于是否有可供显示的博客文章。因此,“博客 - 文章”小组件将“Blog Post”定义为必需的上下文。

为什么要使用小组件上下文

无论是创建新应用程序还是向现有应用程序添加小组件,使用小组件上下文都很重要。如果您的小组件要求某些项目在上下文中才能正常运行,则小组件上下文允许您强制实施此逻辑,并防止终端用户将小组件添加到它们无法正常工作的页面。

创建小组件上下文提供程序

要添加小组件上下文,请实现 IScriptedContentWidgetContextProvider 接口(这需要引用 Limyee.ScriptedWidgets.dll)。IScriptedContentWidgetContextProvider 接口扩展了 IPlugin 以添加对小组件上下文提供程序的支持。

首先,我们需要确定我们正在定义哪些上下文。此方法应返回由此上下文提供程序管理的所有上下文。每个上下文项都使用名称和 Id 进行定义。名称显示在小组件编辑的用户界面中,Id 在内部用于标识此上下文。在我们的示例中,我们将定义一个名为“Blog Example”的小组件上下文。

IEnumerable<ContextItem> IScriptedContentWidgetContextProvider.GetSupportedContextItems()
{
    return new List<ContextItem>()
    {
        new ContextItem("Blog Example", BlogExampleContextItemId)
    };
}

接下来,我们实现 HasContextItem 方法。如果提供的页面上存在提供的上下文标识符,则此方法应返回 true。对于无法识别的上下文标识符,提供程序应始终返回 false。对于已识别的上下文标识符,仅当页面上存在的上下文描述对象有效时,提供程序才应返回 true。

bool IScriptedContentWidgetContextProvider.HasContextItem(System.Web.UI.Page page, Guid contextItemId)
{
    return contextItemId == BlogExampleContextItemId && Apis.Get<IUrl>().CurrentContext != null &&
        Apis.Get<IUrl>().CurrentContext.ContextItems.GetAllContextItems()
            .Any(item => item.ContentTypeId == Apis.Get<IBlogs>().ApplicationTypeId);
}

以下是我们的小组件上下文提供程序的完整源代码:

using System;
using System.Collections.Generic;
using System.Linq;
using Limyee.Extensibility;
using Limyee.Extensibility.Api.Version1;
using Limyee.Extensibility.UI.Version1;
using Limyee.Extensibility.Version1;

namespace Samples
{
    public class BlogExampleWidgetContext : IScriptedContentWidgetContextProvider
    {
        private readonly Guid BlogExampleContextItemId = new Guid("BEB12B07-494D-4670-A084-126965D93B89");

        IEnumerable<ContextItem> IScriptedContentWidgetContextProvider.GetSupportedContextItems()
        {
            return new List<ContextItem>()
            {
                new ContextItem("Blog Example", BlogExampleContextItemId)
            };
        }

        bool IScriptedContentWidgetContextProvider.HasContextItem(System.Web.UI.Page page, Guid contextItemId)
        {
            return contextItemId == BlogExampleContextItemId && Apis.Get<IUrl>().CurrentContext != null &&
                Apis.Get<IUrl>().CurrentContext.ContextItems.GetAllContextItems()
                    .Any(item => item.ContentTypeId == Apis.Get<IBlogs>().ApplicationTypeId);
        }

        string IPlugin.Description
        {
            get { return "使小组件仅显示在上下文中有博客应用程序的页面。"; }
        }

        void IPlugin.Initialize() { }

        string IPlugin.Name
        {
            get { return "博客示例小组件上下文"; }
        }
    }
}

将此示例部署到 Limyee 电商平台并启用插件后,您将能够在小组件工作室中开发小组件时选择上下文提供程序。

在站点中布置页面时,任何有“Blog Example”上下文的小组件将只能添加到博客的页面。

  • 分享
  • 历史
  • 更多
  • 取消
相关
推荐
Copyright © 2021 东莞市连易网络科技有限公司. All rights reserved.
Powered by Limyee Commerce System(3.0.0). © 2014 - 2025 Limyee Inc.