我想使用Ektron的JS.RegisterJSInclude加载我的JavaScript文件,但我想设置JavaScript的加载顺序。
Ektron.Cms.CommonApi _api = new Ektron.Cms.CommonApi();
// I'd like this to be called first every time
JS.RegisterJSInclude(this, _api.SitePath + "/js/first.js", "FirstJS");
JS.RegisterJSInclude(this, _api.SitePath + "/js/atk.js", "AdobeTypeKitJS");
JS.RegisterJSInclude(this, _api.SitePath + "/js/file2.js", "File2JS");
// I'd like this to be called last every time
JS.RegisterJSInclude(this, _api.SitePath + "/js/last.js", "LastJS");
这些文件将在调用它们时加载。我想要做的是控制它们被调用的顺序,无论是从MasterPage,WebForm还是小部件调用它。例如,如果我打电话给
Ektron.Cms.CommonApi _api = new Ektron.Cms.CommonApi();
JS.RegisterJSInclude(this, _api.SitePath + "/js/widget.js", "WidgetJS");
在小部件中,我希望它始终位于'LastJS'之前。
Ektron v8.6可以实现吗?
答案 0 :(得分:0)
您没有指定版本,所以我将假设使用完整的Framework API
归结为页面事件执行顺序 - 无法手动指定优先级顺序。
但是,如果在窗口小部件Init事件中加载/js/widget.js并在页面Load事件中加载其他事件,则应首先加载widget.js,例如。我不确定这对于你上面描述的内容是否理想 - 你可能要求在上面的代码中将 之间的加载小部件JS,这是不可能的。至少,不是开箱即用。
但是,您可以使用自定义解决方案做些事情。例如,像这样的东西应该工作,虽然我还没有测试过。我正在使用HTTPContext将对象从一个事件传递到另一个事件 - 这将在小部件,页面,主控等工作。
// adding javascript objects to http context in init function on page/widget
protected void Page_Init(object sender, EventArgs e)
{
List<JSObject> AddOns;
object StoredAddOns = HttpContext.Current.Items["JSAddOns"];
if (StoredAddOns == null)
{
AddOns = new List<JSObject>();
}
else
{
AddOns = (List<JSObject>)StoredAddOns;
}
// javascript being added to the list in the wrong order intentionally
AddOns.Add(new JSObject() { name = "LastJS", path = "/js/last.js", priority = 10 });
AddOns.Add(new JSObject() { name = "WidgetJS", path = "/js/widget.js", priority = 5 });
HttpContext.Current.Items["JSAddOns"] = AddOns;
}
// reading js objects from httpcontext and adding them to the page using framework ui api
// this could just as easily be prerender - as long as the event that reads the objects is after the event that writes them
protected void Page_Load(object sender, EventArgs e)
{
object StoredAddOns = HttpContext.Current.Items["JSAddOns"];
if (StoredAddOns != null)
{
var AddOns = (List<JSObject>)StoredAddOns;
// using LINQ to order the JS objects by custom-defined priority setting
var SortedAddOns = AddOns.OrderBy(j => j.priority);
// looping through items in priority order to add to the page
foreach (var js in SortedAddOns)
{
Ektron.Cms.Framework.UI.JavaScript.Register(this, js.path, true);
}
}
}
// the custom js object
public class JSObject
{
public int priority { get; set; }
public string path { get; set; }
public string name { get; set; }
public JSObject()
{
priority = 0;
path = string.Empty;
name = string.Empty;
}
}
旁注:如果您要使用SitePath,请不要使用/启动下一个字符串。否则,路径将出现//js/widget.js,假设您的Ektron应用程序位于您的站点的根目录中,或者/path//js/widget.js,如果它位于子文件夹或子应用程序中。