首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Sitecore 8呼叫自定义组件Javascript方法

Sitecore 8呼叫自定义组件Javascript方法
EN

Stack Overflow用户
提问于 2015-10-09 11:43:07
回答 1查看 1.1K关注 0票数 0

我的问题有点类似于以下未回答的问题。(但不确定) Sitecore 8 SPEAK: Getting an Error When calling a Method in JS File

我正在使用Sitecore8

在我的页面上有一个按钮,在它的单击事件上,我想调用定制数据源组件的add()。

布局:

JS网页代码:

代码语言:javascript
复制
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代码:

代码语言:javascript
复制
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:

代码语言:javascript
复制
@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>

当页面加载时:

  • 它显示来自自定义组件Init的警报
  • 然后显示来自主页的Init的警报
  • 按一下按钮,它会显示警报,然后在"app“上出现错误。

有一点我错过了。任何帮助都将不胜感激..。如果你还需要更多的投入,请告诉我。

提前感谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-09 12:52:44

app只有在调试模式下才可用,所以我避免使用它,而是使用"this“。

从您的代码示例中可以看出,您正在调用app.Add(),在您的pageCode上没有添加函数,这就是您的代码所要做的。相反,您需要访问组件的Add方法。

相反,要访问组件中的事件,需要调用如下函数:

this.ComponentID.Add();

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

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33037422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档