2015-10-09 28 views
0

我的問題是有點類似跟隨着懸而未決的問題。 (不知道雖然) Sitecore 8 SPEAK: Getting an Error When calling a Method in JS FileSitecore的8 SPEAK - 調用自定義組件上的JavaScript方法

我使用Sitecore8

在我的頁面上有一個按鈕,並在其click事件我要調用add()的自定義數據源組件。

佈局:

Layout looks like this

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> 

頁面加載時:

  • 它顯示警報從自定義組件INIT
  • 然後顯示了從主機頁面的Init警報
  • 在按鈕單擊它顯示警報,之後給出了「錯誤應用」。

有一點,我失蹤..任何幫助將不勝感激..請讓我知道,如果你需要任何輸入。

在此先感謝!因此,使用ID避免這種情況,用「這個」,而不是

回答

1

應用程序僅在調試模式下可用。

從你的代碼示例看來,你在呼喚app.Add(),對您的pageCode沒有添加功能,這是你的代碼做什麼。相反,您需要訪問組件的添加方法。

而不是你的組件內訪問事件要這樣調用該函數:

this.ComponentID.Add();

我有一個自定義SPEAK組件的例子在這裏您可以參閱如何創建組件。 https://github.com/sobek1985/MikeRobbinsSPEAKRichTextEditor

從代碼看來你創建一個JSON數據源,有由Anders一個例子這裏http://laubplusco.net/creating-simple-sitecore-speak-json-datasource/

+0

非常感謝您!我已經在編寫應用程序,查看您提供的第二個鏈接。 出於某種原因,我的變化做出的Javascript不會反映除非我清除瀏覽器緩存。對此有何想法?或者我應該爲此發佈一個不同的問題? –

+0

預期是啊,多數民衆贊成,Sitecore的緩存所有的JavaScript非常好。 – Komainu85