在 Orchard 中添加 jquery 个图书馆
Adding jquery library in Orchard
我正在构建一个没有任何视图的模块,但会在所有页面中插入一些 javascript。这 javascript 需要 JQuery。为此,我想指示 Orchard 它需要加载 jquery 模块。我找到了一些方法来做到这一点,但所有方法都在视图中。
有没有办法告诉 orchard 从控制器加载 jquery 库,或者更好的是从 FilterProvider 加载?
当然,在您的模块中使 ResourceManifest.cs
using Orchard.UI.Resources;
public class ResourceManifest : IResourceManifestProvider
{
public void BuildManifests(ResourceManifestBuilder builder)
{
var manifest = builder.Add();
manifest.DefineScript("MyScript")
.SetUrl("myscript.min.js", "myscript.js") // Or just use one
.SetVersion("1.0.0")
.SetDependencies("jQuery");
}
}
然后只需在 FilterProvider
中包含 MyScript
和资源管理器
public class MyScriptFilter : FilterProvider, IResultFilter
{
private readonly IResourceManager resourceManager;
public MyScriptFilter(IResourceManager resourceManager)
{
this.resourceManager = resourceManager;
}
public void OnResultExecuting(ResultExecutingContext filterContext)
{
if (!(filterContext.Result is ViewResult) || !UI.Admin.AdminFilter.IsApplied(filterContext.RequestContext))
{
// Control where when it should and shouldn't appear, like exclude admin and any non-ViewResult
return;
}
this.resourceManager.Require("script", "MyScript");
}
public void OnResultExecuted(ResultExecutedContext filterContext)
{
}
}
还没有测试过,所以不确定它是否可以编译,但它应该可以解决问题。
我正在构建一个没有任何视图的模块,但会在所有页面中插入一些 javascript。这 javascript 需要 JQuery。为此,我想指示 Orchard 它需要加载 jquery 模块。我找到了一些方法来做到这一点,但所有方法都在视图中。
有没有办法告诉 orchard 从控制器加载 jquery 库,或者更好的是从 FilterProvider 加载?
当然,在您的模块中使 ResourceManifest.cs
using Orchard.UI.Resources;
public class ResourceManifest : IResourceManifestProvider
{
public void BuildManifests(ResourceManifestBuilder builder)
{
var manifest = builder.Add();
manifest.DefineScript("MyScript")
.SetUrl("myscript.min.js", "myscript.js") // Or just use one
.SetVersion("1.0.0")
.SetDependencies("jQuery");
}
}
然后只需在 FilterProvider
MyScript
和资源管理器
public class MyScriptFilter : FilterProvider, IResultFilter
{
private readonly IResourceManager resourceManager;
public MyScriptFilter(IResourceManager resourceManager)
{
this.resourceManager = resourceManager;
}
public void OnResultExecuting(ResultExecutingContext filterContext)
{
if (!(filterContext.Result is ViewResult) || !UI.Admin.AdminFilter.IsApplied(filterContext.RequestContext))
{
// Control where when it should and shouldn't appear, like exclude admin and any non-ViewResult
return;
}
this.resourceManager.Require("script", "MyScript");
}
public void OnResultExecuted(ResultExecutedContext filterContext)
{
}
}
还没有测试过,所以不确定它是否可以编译,但它应该可以解决问题。