我构建的模块没有任何视图,但在所有页面中都插入了一些javascript。这个javascript需要JQuery。为此,我想指示Orchard需要加载jquery模块。我找到了一些方法来做到这一点,但所有这些都在视野中。
有没有办法告诉果园从控制器加载jquery库,或者更好地从FilterProvider加载?
答案 0 :(得分:1)
当然,在您的模块中创建 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");
}
}
然后在您的MyScript
FilterProvider
资源管理器
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)
{
}
}
Haven没有对此进行测试,因此不确定它是否可以编译,但它应该可以解决问题。