gpt4 book ai didi

Sitecore SPEAK 初学者在 Javascript 包含方面遇到问题

转载 作者:行者123 更新时间:2023-12-02 19:27:55 26 4
gpt4 key购买 nike

我正在尝试编写我的第一个简单的 Sitecore Speak 组件。

没什么特别的,我只需要开始。因此,我在 Visual Studio 中创建了 Speak 组件,并在 Sitecore 中创建了指向我的组件的渲染。这一切都是开箱即用的。我将渲染插入到 SPEAK 布局中,当我访问该页面时,我收到一条错误消息 enter image description here

有人知道为什么吗?我需要做什么?我正在使用 Sitecore 7.5。

我的 cshtml 看起来像这样:

@using Sitecore.Mvc
@using Sitecore.Mvc.Presentation
@using Sitecore.Web.UI.Controls.Common.UserControls
@model RenderingModel
@{
var rendering = Html.Sitecore().Controls().GetUserControl(Model.Rendering);
rendering.Class = "sc-Test2";
rendering.Requires.Script("client", "Test2.js");

var htmlAttributes = rendering.HtmlAttributes;
}
<div @htmlAttributes>

</div>

最佳答案

基本上,这里的问题是 Sitecore SPEAK 无法找到该组件的 javascript。

看来您的 SPEAK 组件渲染和 javascript 位于您自己的目录中,这是最佳实践。您所要做的就是告诉 SPEAK 在哪里寻找您的组件。这是在 Include 文件夹内的 Sitecore SPEAK 配置中控制的。

这里是一个补丁包含文件的示例,用于在目录中进行补丁,以便 SPEAK 查找 javascript 组件。

将源设置为包含自定义组件的目录。Deep = true 参数将扫描子目录。另请注意本示例中的类别名称“MikeRobbins”。选择任何对您的组件有意义的内容,我会避免使用 Sitecore,以免影响 Sitecore 标准组件。

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<pipelines>
<speak.client.resolveScript>
<processor type="Sitecore.Resources.Pipelines.ResolveScript.Controls, Sitecore.Speak.Client">
<sources hint="raw:AddSource">
<source folder="/sitecore/shell/client/MikeRobbins/Layouts/Renderings" deep="true" category="mikerobbins" pattern="*.js,*.css" />
</sources>
</processor>
</speak.client.resolveScript>
</pipelines>
</sitecore>
</configuration>

更新您的组件 CSHTML 并将 userControl.Requires.Script("mikerobbins"..... 部分设置为您在上面选择的类别名称。请参阅下面的示例。

@using Sitecore.Mvc
@using Sitecore.Mvc.Presentation
@using Sitecore.Web.UI.Controls.Common.UserControls
@model RenderingModel
@{
var userControl = Html.Sitecore().Controls().GetUserControl(Model.Rendering);
userControl.Requires.Script("mikerobbins", "JsonDataSource.js");
userControl.Attributes["type"] = "text/x-sitecore-jsondatasource";
userControl.DataBind = "Json: json";
var htmlAttributes = userControl.HtmlAttributes;
}
<script @htmlAttributes>
</script>

我的项目中可以看到这样的示例。 https://github.com/sobek1985/SitecoreSPEAKBulkRolePermissions

希望这对您有帮助,有任何问题请给我留言。

关于Sitecore SPEAK 初学者在 Javascript 包含方面遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26438383/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com