Sitecore 8 SPEAK - 调用自定义组件 Javascript 方法

Sitecore 8 SPEAK - Calling Custom components Javascript method

我的问题有点类似于followin unanswered question。 (虽然不确定) Sitecore 8 SPEAK: Getting an Error When calling a Method in JS File

我正在使用 Sitecore8

在我的页面上有一个按钮,我想在它的点击事件中调用自定义数据源组件的 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>

页面加载时:

我遗漏了一些内容。如有任何帮助,我们将不胜感激。如果您需要更多信息,请告诉我。

提前致谢!

app 仅在调试模式下可用,因此请避免使用它,请改用 "this"。

从您的代码示例看来,您正在调用 app.Add(),您的 pageCode 上没有添加函数,这是您的代码正在执行的操作。相反,您需要访问组件的添加方法。

要访问组件内的事件,您需要像这样调用函数:

this.ComponentID.Add();

我这里有一个自定义 SPEAK 组件的示例,您可以参考如何创建该组件。 https://github.com/sobek1985/MikeRobbinsSPEAKRichTextEditor

从代码看来,您创建了一个 JSON 数据源,这里有一个 Anders 的示例 http://laubplusco.net/creating-simple-sitecore-speak-json-datasource/