我的问题有点类似于以下未回答的问题。 (虽然不确定) Sitecore 8 SPEAK: Getting an Error When calling a Method in JS File
我使用的是Sitecore8
在我的页面上有一个按钮,在其click事件中我想调用自定义数据源组件的add()。
布局:
页面的JS代码:
define(["sitecore"], function (Sitecore) {
var JsonListPage = Sitecore.Definitions.App.extend({
initialized: function () {
alert('Inside Json PageList Init');
},
loadData: function () {
alert('Button clicked');
app.add();
}
});
return JsonListPage;
});
自定义数据源组件的JS代码:
define(["sitecore"], function (Sitecore) {
var model = Sitecore.Definitions.Models.ControlModel.extend({
initialize: function (options) {
this._super();
this.set("json", null);
alert('Inside Jsondatasource Init');
},
add: function (data) {
var json = this.get("json");
if (json === null)
json = new Array();
// this is done because array.push changes the array to an object which then do no work on the SPEAK listcontrol.
var newArray = new Array(json.length + 1);
for (var i = json.length - 1; i >= 0; i--)
newArray[i + 1] = json[i];
newArray[0] = data;
this.set("json", newArray);
}
});
var view = Sitecore.Definitions.Views.ControlView.extend({
initialize: function (options) {
this._super();
this.model.set("json", null);
}
});
Sitecore.Factories.createComponent("JsonDatasource", model, view, ".x-sitecore-jsondatasource");
});
自定义组件的.cshtml:
@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("client", "JsonDatasource.js");
userControl.Class = "x-sitecore-jsondatasource";
userControl.Attributes["type"] = "text/x-sitecore-jsondatasource";
userControl.DataBind = "Json: json";
var htmlAttributes = userControl.HtmlAttributes;
}
<div @htmlAttributes>
am here again
</div>
页面加载时:
有一点我不知道......任何帮助都会受到赞赏..如果您需要更多信息,请告诉我。
提前致谢!
答案 0 :(得分:1)
应用程序仅在调试模式下可用,因此id避免使用它,而是使用“this”。
从您的代码示例中看来,您正在调用app.Add(),pageCode上没有Add函数,这就是您的代码正在执行的操作。相反,您需要访问组件的添加方法。
要访问组件中的事件,您需要调用此函数:
this.ComponentID.Add();
我在这里有一个自定义SPEAK组件的示例,您可以参考如何创建组件。 https://github.com/sobek1985/MikeRobbinsSPEAKRichTextEditor
从代码看来你创建了一个JSON数据源,Anders在这里有一个例子http://laubplusco.net/creating-simple-sitecore-speak-json-datasource/